- Home /
How to rotate gun only on X and Y axis?
I have a game where the player is stationary and enemies walk at them. I have a gun in front of the player view that moves left and right / up and down with input from the mouse. However, The Z rotation of the gun always makes itself flip upside down after enough mouse movement.
Here's the code.
 public class RotateGun : MonoBehaviour
 {
     [SerializeField] private float horizontalSpeed = 2.0f;
     [SerializeField] private float verticalSpeed = 2.0f;
 
     // Update is called once per frame
     void Update()
     {
         float h = horizontalSpeed * Input.GetAxis("Mouse X");
         float v = verticalSpeed * Input.GetAxis("Mouse Y");
                 
         transform.Rotate(v, h, 0);        
     }
 }
If anyone can send me in the right direction, I'd appreciate it.
Answer by fax58 · Apr 23 at 10:16 AM
Did you try to freeze the axis in the rigidbody?

That's what I tried initially and, surprisingly, it didn't work.
Your answer
 
 
             Follow this Question
Related Questions
RotateAround object around player when pushing button. 1 Answer
managing rotation of Turret on two axis 1 Answer
Coroutine not starting from Update 2 Answers
How can I make an object always look at the mouse? 2 Answers
When rotating an object the resulting angle is dfferent from what I set. 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                