- Home /
Limiting the X and Y of a rotation to 0
I cant figure out how to do it. Whenever i collide into another game object it sends me spinning on the x and y axis. My game is clamped on the y axis and that stays at 0. my controller for the player uses the Y rotation so this code below doesnt work because then i cant turn my player:
void Update () {
float yMovement = (turnSpeed * 20f * Time.deltaTime * Input.GetAxis("Horizontal"));
transform.Rotate (0,yMovement,0);
transform.rotation = Quaternion.Euler(0,yMovement,0);
}
}
what i need is to clamp the X and Z rotation to 0 but leave the Y free to move anywhere.
Tried $$anonymous$$athf.clamp?
http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Clamp.html
I'm a bit puzzled here without the full picture. You write about collisions. Is your object a rigidbody, a character controller? If a rigidbody, have you tried to freeze x and z rotations in the constraints?
You can try the following line of code in place of lines 6 and 7:
transform.up = Vector3.up;
Yes i am using rigid bodies. freezing contraints doesnt work for me, not sure why. I forgot to say my above code fixes the problem, but then it doesnt let me turn left or right. I need some sort of code similar to this:
transform.rotation = Quaternion.Euler(0,transform.rotation.y,0);
You probably want this:
transform.up = Vector3.up;
transform.Rotate(0,y$$anonymous$$ovement,0);
This first straightens the player to align with the world 'Y', and then it does a relative rotation around the local 'Y' axis. You may need to do it in LateUpdate().
The line of code you outlined:
transform.rotation = Quaternion.Euler(0,y$$anonymous$$ovement,0);
...will not work because it is an absolute rotation that will only vary based on your Input.GetAxis().
Answer by Krey · Oct 11, 2013 at 11:06 PM
Do you want to register the collisions at all? If not, you could just avoid it entirely by using Unity's layer-based collision matrix: Layer-based Collision Matrix
That would allow you to selectively prevent other objects from colliding with the player, each other, etc...
Your answer
Follow this Question
Related Questions
Quaternion values at instantiation. 1 Answer
Rotate on 2 different axes 1 Answer
How to smoothly align an object to a surface, but only partially 2 Answers
Rotation 90.0001 and 1.001719 bugs! 1 Answer