unity – How to follow an object with the player without stepping out the waypoint line Unity3D?

How to follow an object with the player without stepping out the waypoint line? I added four waypoints with a debug.line and tried doing some simple waypoint mechanic. I wanted the object to move from one waypoint to another, depending on where the player is moving. If one of the waypoints is Y-axis higher than the other waypoints, the object will move in the slope direction.

  • How the code works below is that every point has a physics.checkbox to print out the point’s number. If the object collides with a point with a digit of 0, it’ll move from that x and z-axis but can’t go less or more than that specific point. However, when the player jumps at the top of the box and goes to the opposite side, the object won’t register where I am already. It’ll only register its old position, and for it to go to my new position, it has to hit a point first.

  • It can’t move in the slope direction, which is also what I wanted.

Is there a clean and effective way of doing this?


This an example I wanted to achieve:
enter image description here


My progress so far:

enter image description here


Code: Manager

public Transform() point;
public Transform obj;
public Transform player;

public int pointDigit = 0;

 void Update()
    {
    DrawLine();

    //Point 0
    if(pointDigit == 0)
    {
        if (player.position.z > point(pointDigit).position.z)
        {
            obj.position = new Vector3(point(pointDigit).position.x, point(pointDigit).position.y, player.position.z);
        }

        if (obj.position == point(pointDigit).transform.position)
        {
            obj.position = new Vector3(point(pointDigit).position.x, point(pointDigit).position.y, point(pointDigit).position.z);
        }

        if (player.position.x > point(pointDigit).position.x)
        {
            obj.position = new Vector3(player.position.x, point(pointDigit).position.y, point(pointDigit).position.z);
        }
    }
    //Point 1
    else if (pointDigit == 1)
    {
        if (player.position.z < point(pointDigit).position.z)
        {
            obj.position = new Vector3(point(pointDigit).position.x, point(pointDigit).position.y, player.position.z);
        }

        if (obj.position == point(pointDigit).transform.position)
        {
            obj.position = new Vector3(point(pointDigit).position.x, point(pointDigit).position.y, point(pointDigit).position.z);
        }

        if (player.position.x > point(pointDigit).position.x)
        {
            obj.position = new Vector3(player.position.x, point(pointDigit).position.y, point(pointDigit).position.z);
        }
    }
    //Point 2
    else if (pointDigit == 2)
    {
        if (player.position.z < point(pointDigit).position.z)
        {
            obj.position = new Vector3(point(pointDigit).position.x, point(pointDigit).position.y, player.position.z);
        }

        if (obj.position == point(pointDigit).transform.position)
        {
            obj.position = new Vector3(point(pointDigit).position.x, point(pointDigit).position.y, point(pointDigit).position.z);
        }

        if (player.position.x < point(pointDigit).position.x)
        {
            obj.position = new Vector3(player.position.x, point(pointDigit).position.y, point(pointDigit).position.z);
        }
    }
    //Point 3
    else if (pointDigit == 3)
    {
        if (player.position.z > point(pointDigit).position.z)
        {
            obj.position = new Vector3(point(pointDigit).position.x, point(pointDigit).position.y, player.position.z);
        }

        if (obj.position == point(pointDigit).transform.position)
        {
            obj.position = new Vector3(point(pointDigit).position.x, point(pointDigit).position.y, point(pointDigit).position.z);
        }

        if (player.position.x < point(pointDigit).position.x)
        {
            obj.position = new Vector3(player.position.x, point(pointDigit).position.y, point(pointDigit).position.z);
        }
    }
    }

void DrawLine()
    {
        for (int i = 0; i < point.Length; i++)
        {
            if (i < point.Length - 1)
            {
                Debug.DrawLine(point(i).transform.position, point(i + 1).transform.position, Color.green);
            }
            else
            {
                Debug.DrawLine(point(point.Length - 1).transform.position, point(0).transform.position, Color.green);
            }
        }
    }

Code: Waypoint

public LayerMask layerMask;

public int point;
public float radius;

bool isTrigger;


private void Update()
{
    isTrigger = Physics.CheckSphere(transform.position, radius, layerMask);

    if (isTrigger)
    {
        print(node);
        PathFollower.Instance.pointDigit = point;
    }
}