How do you get the X, Y and Z position of a game object?

Hello!
Right now I am trying to make a camera that will follow a game object.

Is there any way to get the specific X, Y and Z from it? (It would help if there was a way to use it as a variable)

That’s about it.

Thanks!

A GameObject doesn’t actually do anything, the components in it do. The Transform component contains all that information i.e. position, rotation, scale and a link to the parent/children Transforms so use that.

It does sound like you’re asking a basic C# question on how to access properties etc but it’s hard to tell.

Yeah, sort of.

Is there anyway i could use the X, Y, Z in a way like this: ```
cam.transform.position = (X, Y, Z)


Thanks!

You cannot just make up your own syntax no. As you can see from the API docs, “position” is a Vector3 so you need to create one of those and assign it.

transform.position = new Vector3(X, Y, Z);

In the same way as when you read it, you get a Vector3 with XYZ properties.

2 Likes

This is the literal first lesson in almost every Unity tutorial. You should do some of those.

3 Likes

Got it.

I already know about vector 3’s and such but still thanks for the help!

I’m still quite new to unity but i have coded in other languages.

Thanks!

Fixed it: ```
{
public GameObject cam;
public Vector3 camOffset;
void Update()
{
cam.transform.position = transform.position + camOffset;
}
}


Thanks for the help!
3 Likes