How do I get a Game Objects position to use for a Rigid Body?

I wan’t to use rb.AddForce(playerX, 0, playerZ);
But I can’t seem to figure out how to get the players X and Y coordinates to use in a rigid body. All I want to do is have a GameObject with a Rigidbody to follow the player. Is there a way to do this?

There’s a lot of things to cover here.

First, a force is NOT a position. It’s a force. A force is also NOT a velocity. It’s a force.

The second is, GameObjects don’t have position, they store it in their Transform component. This is available in GameObjects and Component-derived objects as the .transform shortcut.

Third is, Rigidbodies ALSO store their position internally as part of the Physics system, COMPLETELY separate from the scene. They do sync it to the Transform component each frame (see timing below), or when you want them to.

So, to answer your question,

  • get the Transform component from the thing you are chasing

  • read the position out of the position field of the Transform

  • subtract the position from the follower’s position to come up with an offset

  • decide how to move in that direction:
    -----> MoveTowards?
    -----> set position directly (always use .MovePosition() on Rigidbodies or you WILL glitch physics)
    -----> AddForce (careful! it will accelerate unbounded and fly past it!)
    -----> set the Rigidbody.Velocity?
    -----> something else?

  • do the computation and make it happen accordingly

I recommend to understand all this a bit better you go and work through three or four “following AI” tutorials and grasp the basics, then work through some tutorials on moving things with Rigidbodies via forces, velocities or movements (all three have different behaviors).

Here is some timing diagram help: