- Home /
Restricting the movment in the Zaxis
Ok i dont want another look at this 2D game tutorial from unity.pdf that space game. But can someone please help me :)
All i want is no movment in the z axis , i still want the object to be able to rotate left and right but not backwards and forward .. etc so its true 2D movment..
just a simple script to apply?
Thanks..
Answer by pfranza · Jan 20, 2011 at 08:19 PM
You can restrict the movement of a object within the Update() method
If you want to restrict the movement of the Z axis to be within certain bounds you can use:
function Update() {
transform.position.z = Mathf.Clamp(transform.position.z, -10, 10);
}
the above sample will restrict the z axis to be between -10 and 10
If you want to disable the movement of the object within a certain axis entirely you can do something like the following
function Update() {
transform.position.z = 0;
}
This way after each update cycle regardless to what happened to that object the z position resets to zero. Movement to any axis can be restricted similarly.
Ok thankyou that worked :) But if i want to stop for example a cube from rotating forwards and backwards, while still alowing it to rotate from left to right, how would i do this?!
would it be:
function Update() { transform.position.z = 0; transform.rotation.z = 0; }
Answer by jonas-echterhoff · Jan 20, 2011 at 04:39 PM
Unity 3.2 will add built-in support for this in the Rigidbody component.
Until then, a simple script setting respective transform.position and transform.rotation coordinates to 0 (or whatever you want) in Update or FixedUpdate should do.
Your answer
Follow this Question
Related Questions
Help with 2D AI scripting 2 Answers
Moving camera by z axis in 2D 1 Answer
2D Top Down Character Controller 1 Answer
Moving object disappears then comes back 1 Answer
[2D] Moving the player 1 tile at a time using rigidbody movement 0 Answers