Runtime Trail

Today we added a custom trail for our Rope Weapon.

 

p9

 

public void UpdateRopeTrail()

    {

        DestroyTrail();

        StartPoint = _ropeTrailOrigin.transform.position; // projectile position

        EndPoint = _projectileSpawner.transform.position; // rope weapon pos

        // make a single last trail unit at weapon position

        _lastTrailUnit.transform.position = _projectileSpawner.transform.position;

        RopeTrailUnitCount = Mathf.CeilToInt( (_distanceFromPlayer/RopeTrailSingleUnitWidth) );

        Vector3[] pointsArray = new Vector3[RopeTrailUnitCount];

        GetPathPointsFromProjectileToPlayer(pointsArray);

        TrailUnitsArray = new GameObject[pointsArray.Length];

        for (int i = 0; i < pointsArray.Length; i++)

        {

            // instantiate rope trailunits on points

            TrailUnitsArray[i] = (GameObject)Instantiate(RopeTrailGameobject, pointsArray[i], new Quaternion(0000) );

            TrailUnitsArray[i].transform.parent = transform;

        }

    }

    public void DestroyTrail()

    {

        if (RopeTrailUnitCount == 0)

            return;

        for (int i = 0; i < TrailUnitsArray.Length; i++)

        {

            Destroy (TrailUnitsArray[i]);

        }

    }

    public void GetPathPointsFromProjectileToPlayer(Vector3[] pointsArray)

    {

        float PosX = StartPoint.x;

        float PosY = StartPoint.y;

        float HeightFactor = RopeTrailSingleUnitHeight;

        float verticalDistance = 0;

        float horizontalDistance = 0;

        bool SubX = false;

        bool SubY = false;

        int pointsCount = pointsArray.Length;

        if (EndPoint.x > StartPoint.x)

        {

            horizontalDistance = EndPoint.x – StartPoint.x;

            SubX = false;

        }

        else

        {

            horizontalDistance = StartPoint.x – EndPoint.x;

            SubX = true;

        }

        if ( EndPoint.y > StartPoint.y )

        {

            verticalDistance = EndPoint.y – StartPoint.y;

            SubY = false;

        }

        if ( StartPoint.y > EndPoint.y )

        {

            verticalDistance =  StartPoint.y – EndPoint.y;

            SubY = true;

        }

        HeightFactor = (RopeTrailSingleUnitHeight * verticalDistance)/(horizontalDistance);

        for (int i = 0; i < pointsArray.Length; i++)

        {

            if (SubX)

                PosX -= RopeTrailSingleUnitWidth;

            else

                PosX += RopeTrailSingleUnitWidth;

            pointsArray[i].x = PosX;

            if (SubY)

            {

                PosY -= HeightFactor;

            }

            else

            {

                PosY += HeightFactor;

            }

            pointsArray[i].y = PosY;

            pointsArray[i].z = StartPoint.z;

        }

    }

Thanks for reading.

if you want to check out some of our projects please subscribe to our YouTube channel:

You can check up our work in progress games here:

If you are looking for solutions to game development issues see our blogs here:

If you like our projects please sign up for our newsletter at the bottom of this Page.

Leave a Reply