The z position of an object

Michiel

New member
Joined
Jul 7, 2019
Messages
1
Programming Experience
Beginner
Hello

I have a question about the position of an object.
I want to let my ground follow the player object on top of it.
But I only want it to follow and the z of the player object and right now I only know how to let it follow the x, y and the z.
Here's the code that I have right now:
C#:
public class StayUnderPlayer : MonoBehaviour
{
    public Transform player;
    public Vector3 offsetG;

    void Update()
    {
        transform.position = player.position - offsetG;
    }
}
Could someone help me out?
 
Last edited by a moderator:
You could do either:
- the brute force of just setting x and y based on the player, and then setting z to whatever your ground level is supposed to be; OR
- copy the player's transform, then use the Translate() method to force the z to whatever your ground level is supposed to be.
 
I am going to assume this is a GameDev question for a Unity 3D game?

If case in point, then you'd be best looking at the Transform. properties to get/set the z location from/of your player object.

Never had you down as a Game Dev Skydiver...
 
Don't assume the ground level in Unity, it can be acquired by your player object as It has been described twice how to do.
 
Back
Top Bottom