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 :slight_smile:

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

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 =)

Use UnityEditor namespace. There should be Gizmo customizer.

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);