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?


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.

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.