I am building a 2D game and want to change the location of the player when the y value gets lower than -10, I do this like this:
maxFallDistance = -10;
if (transform.position.y <= maxFallDistance)
{
Debug.Log("Out of bounds!");
}
and this works, so i do know how to recognize when the player’s y position is lower than -10, but i can’t change the x and y. to another value, how do i do this?
PS: I’m really new to Unity, but i really like it!
I think the problem you’ve encountered is that you can’t say transform.position.y = 10
right?
The solution is to create a new Vector3:
transform.position = new Vector3(0, 0, 0);
which will set the x and y to zero. If you just want to change the y value, you could do:
transform.position = new Vector3(transform.position.x, 0, transform.position.z);
and change the zero to whatever you want the y value to be.
I’m guessing you use C# and it’s complaining about editing transform.position.y directly?
Store it in a temporary variable that you cange, and then assign it back:
Vector3 pos = transform.position;
pos.x = 12;
transform.position = pos;
If you want the object out of bounds to “teleport” to a specific position use:
transform.position = Vector3(x, y, z);
*feel in the x, y, and z values with your choosing
More info on Vector3’s can be found here:
link text