How to follow objects

I want the Player follow the Ball and get it. Then with the Ball I want the Player follow the Post. I’ve done two targets to be followed sequentially but the Player skips the first target and follows the second target (Post).
My code is like this:

 void Start()
    {
        transformBall = GameObject.Find("Ball").transform;
        transformPost = GameObject.Find("Post").transform;
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        if ((transform.position - targetBall.position).magnitude > EPSILON)
        {
            animator.SetBool("IsMoving", true);

            Vector3 lookAtPosition = transformBall.position;
            lookAtPosition.y = transform.position.y;
            transform.LookAt(lookAtPosition);
  
            Vector3 movedirection = transform.position - targetBall.position;
            Vector3 moveSpeed = new Vector3(movedirection.normalized.x * movementSpeed * Time.deltaTime, 0, movedirection.normalized.z * movementSpeed * Time.deltaTime);
            transform.position -= moveSpeed;
        }
        else
        {
            animator.SetBool("IsMoving", false);
        }


        if ((transform.position - targetPost.position).magnitude > EPSILON)

        {
            animator.SetBool("IsMoving", true);

            Vector3 lookAtPosition = transformPost.position;
            lookAtPosition.y = transform.position.y;
            transform.LookAt(lookAtPosition);
     
            Vector3 movedirection = transform.position - targetPost.position;
            Vector3 moveSpeed = new Vector3(movedirection.normalized.x * movementSpeed * Time.deltaTime, 0, movedirection.normalized.z * movementSpeed * Time.deltaTime);
            transform.position -= moveSpeed;
        }
        else
        {
            animator.SetBool("IsMoving", false);
        }
    }
}

Any idea would be greatly appreciated…

Here is how to debug that:

You must find a way to get the information you need in order to reason about what the problem is.

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

If you want a functioning example of conga line following, see the attached.

8081123–1044938–FollowEnTrail.unitypackage (6.55 KB)

I tried all the solution above. Unfortunately, I couldn’t fix it out.
My code is like this:

  {
        if ((transform.position - target.position).magnitude > EPSILON)
        {
            animator.SetBool("IsMoving", true);

            Vector3 lookAtPosition = transformBall.position;
            lookAtPosition.y = transform.position.y;
            transform.LookAt(lookAtPosition);
           
            Vector3 movedirection = transform.position - target.position;
            Vector3 moveSpeed = new Vector3(movedirection.normalized.x * movementSpeed * Time.deltaTime, 0, movedirection.normalized.z * movementSpeed * Time.deltaTime);
            transform.position -= moveSpeed;
        }
        else
        {
            animator.SetBool("IsMoving", false);
        }

        if ((transform.position - targetPost.position).magnitude > EPSILON)
        {
            animator.SetBool("IsMoving", true);

            Vector3 lookAtPositionP = transformPost.position;
            lookAtPositionP.y = transform.position.y;
            transform.LookAt(lookAtPositionP);

            Vector3 movedirectionP = transform.position - targetP.position;
            Vector3 moveSpeedP = new Vector3(movedirectionP.normalized.x * movementSpeedP * Time.deltaTime, 0, movedirectionP.normalized.z * movementSpeedP * Time.deltaTime);
            transform.position -= moveSpeedP;
        }
        else
        {
            animator.SetBool("IsMoving", false);
        }
    }
}

When I remove the code stack from ```
if ((transform.position - targetPost.position).magnitude > EPSILON)
{

Really?

Didn’t you notice that regardless of the outcome of line 2, you are ALWAYS doing the check in line 19.

So no matter if you get the ball or not (line 2’s block), it is always going to do line 19, which is go to the post.

You may need to make some kind of a state machine that tracks:

  • I am doing nothing
  • I am going to the ball
  • I have got the ball, I am going to the post
  • I am doing nothing again

Otherwise, it is VERY likely that you are greater than EPSILON on both items always.

Sure I’ve noticed that. I am working on how could I implement state machine on my code. I have this but I have no idea of how to apply it on my code:

 var doorKeys: GameObject[];
var openDoor: boolean;
  function Update()
  {
     doorKeys = gameObject.FindGameObjectsWithTag("doorKey");
     if(doorKeys.length == 0)
     {
             openDoor = true;
     }
}

Definitely start with some tutorials. The basic concept of a state machine is fundamental to all kinds of games applications.

For your needs the state could be as simple as a way to track if you have the ball or not, such as perhaps a reference to the ball

If you don’t have the ball (ball reference is null), go get the ball

If you have the ball (the ball reference is not null), then go to the post

ALSO this stuff:

Wherever you got that code from, don’t get anything from there. Abandon it in place immediately. That code is UnityScript, a language that Unity stopped supporting six or seven years ago.

Here’s a great approach to layering on knowledge as you work:

Imphenzia: How Did I Learn To Make Games:

1 Like

I just resolved it. Thanks so much…