I am very new to Unity, so this will sound very silly.
What I want to do is fetch the x and y coordinates of multiple gameObjects, and use those values to determine the behaviour of the script.
I’ve written this pseudo-code to demonstrate:
void Update(){
if (object0.xPosition < object1.xPosition){ //I want to know Unity's lingo for what's happening in this line.
//Behaviour here.
}
}
What is the syntax that I would use to identify the objects’ world position?
on a MonoBehaviour: this.transform.position will give you world position of ‘this’ MonoBehaviour (the one with the script), whereas this.transform.localPosition will give you the local position
so if you have two objects, you can compare their transforms:
public Transform objectA; // if you expose this as 'public' you'll be able to
public Transform objectB; // reference something in the Inspector by dragging in an object
Ah, that makes sense!
But how would I segregate x, y, and z positions?
var pos = transform.position;
Debug.Log(pos.x);
pos.z = 3;
transform.position = pos;
transform.position = new Vector3(1, 2, 3);
if(objectA.position.x < objectB.position.x)
{
// etc
}
2 Likes