- Home /
How to only rotate object on x and z axes, keep the y axis as it is?
I making a shooter game. I have third person and first person cameras and mouse look is enabled only when first person camera is active. When i switch to third person i want to reset the rotation of x and z axis but the character should be looking in the same direction i.e. y axis should not be changed.
Script :-
if(Input.GetButtonDown("Fire3")) { if(tps.active) { tps.active = false; fps.active = true; GetComponent("MouseLook").enabled = !GetComponent("MouseLook").enabled; GameObject.Find("Crosshair").GetComponent("Crosshair").enabled = true; GetComponent("FPSMovement").enabled = !GetComponent("FPSMovement").enabled; GetComponent("TPSMovement").enabled = !GetComponent("TPSMovement").enabled; tps.camera.fieldOfView = 70.0; } else { fps.active = false; tps.active = true; GetComponent("MouseLook").enabled = !GetComponent("MouseLook").enabled; GetComponent("FPSMovement").enabled = !GetComponent("FPSMovement").enabled; GetComponent("TPSMovement").enabled = !GetComponent("TPSMovement").enabled; }
Thank You very much :-)
Answer by zemrod101 · Feb 01, 2013 at 04:27 PM
you need to use a Euler to manually assign the angles
transform.rotation = Quaternion.Euler(0,transform.eulerAngles.y,0);
this should clear the x and z rotations
can u tell me the meaning of the above script? How will the y-axis angle be constant? Thank You :-)
I know it's an old answer but works great for me, my problem was that the model I had needed to remain rotated 90 degrees on the x, then lock the X and y, so just changing the first value to 90 fixed this issue for me. thanks.
Answer by PH-zero · Feb 01, 2013 at 04:10 PM
Hi.
just an idea:
You could do it by checking the y-checkbox on freeze rotation in the Constrains on your Rigidbody.
The Constrains are in the inspector on the rigidbody-Component below Collision Detection
I hope this is helpfull =)
Answer by Plastic Block · Jun 18, 2015 at 07:14 PM
Use UnityEditor namespace. There should be Gizmo customizer.
Answer by Sudhamati_reality · Jul 02, 2020 at 12:00 PM
Try something like this, I wanted to keep z axis rotation freeze. This worked for me.
this.transform.Rotate(Vector3.right, xSpeed, Space.Self); this.transform.Rotate(Vector3.up, ySpeed, Space.World);
Your answer
![](https://koobas.hobune.stream/wayback/20220613091956im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Space.World in MoveObject.js 2 Answers
Can I add axes to the Input Manager via script? 1 Answer
Having an issue with Input.ResetInputAxes 1 Answer
Manage Axes from InputManager 1 Answer
Is it possible to edit 2Dcolliders along the X&Y axes? 0 Answers