Restrict rotation on X Z for rigidbodies

Is there a way to restrict rotation for rigidbodies, to only the y-axis?

The reason is that I’m developing a 2d game, and most of the objects are simple planes with textures on them (Sprites). What I want is for them to collide and affect each others trajectory but without any rotation, in order for the plane to be always perfectly facing the camera.

Is this possible?

AFAIK you can do that with a configurable joint, but I’m not sure if this is the best way to do that. For a full rotation lock you can use http://unity3d.com/support/documentation/ScriptReference/Rigidbody-freezeRotation.html Also you can give this a try http://unity3d.com/support/documentation/ScriptReference/Rigidbody-rotation.html I would definitely use this.

Thanks, but freezing rotation in all axis is not what I intend to do, the sprite is ok to rotate around the y axis.

Thinking about it, I think the LookAt script will work. I just need to place the target way behind the camera. Gonna try it out tonight and see.

Here’s a great script that will limit an object to either X, Y or Z. You can choose which by changing the value in the Inspector:

Not my script, I think I found it on Unity Answers. Props to the author.

I’m not a programmer, but I just did this with this code attached to the gameobject. My code restricts the Z axis and limits the rotation to only the z rotation.

function FixedUpdate () {
	transform.position.z = 0;
	transform.rotation.y = 0;
	transform.rotation.x = 0;
}

I don’t know if this is the most efficient way or proper coding to do it. I am looking into it more right now. If it’s only for a few objects then I think you can get away with it.

Ok to update my findings.

Testing on a 3G and Iphone 4. With about 14 objects with this code attached. The iPhone 4 ran fine, but the 3G got about 5 fps. taking out this code brought it back up to 30+FPS. I changed to using Configurable joints(Component → Physics -->Configurable Joint) on each object and it gave me the same affect with no FPS hit. So I would say if you only using it on a couple GameObjects then the previous code should be fine. If you have a lot of on screen GameObjects needing this then I would use the Configurable Joint.

Here is a thread discussing it.
http://forum.unity3d.com/threads/46901-Limiting-movement-rotation-on-the-z-axis?highlight=restrict+axis