How to rotate an object to the sides without turning or turning 360 degrees?


My problem is that I need an object, if it is my character's arm, the problem is that this as my code, I think it is very broad and I want to place restrictions on it as it does not have a 360 degree turning capacity or that only stay in the front because it is a weapon arm, that is to say that this one will keep facing forward like a gun, but you can rotate it to the sides, up and down just to aim the target.

My code:

objetodejuego[0].transform.Rotate((Input.GetAxis("Mouse Y") * velocidad * Time.deltaTime), (Input.GetAxis("Mouse X") * velocidad * Time.deltaTime), 0, Space.World);
asked by Abraham.P 04.08.2017 в 15:28

1 answer


I found my solution and adapted it to my parameters:

     public float sensitivity = 10f;
 public float maxYAngle = 80f;
 private Vector2 currentRotation;

 void Update () {

     currentRotation.x += Input.GetAxis("Mouse X") * sensitivity;
     currentRotation.y -= Input.GetAxis("Mouse Y") * sensitivity;
     currentRotation.x = Mathf.Clamp(currentRotation.x, -maxYAngle, maxYAngle);
     currentRotation.y = Mathf.Clamp(currentRotation.y, -maxYAngle, maxYAngle);
     transform.rotation = Quaternion.Euler(currentRotation.y,currentRotation.x,0);
     if (Input.GetMouseButtonDown(0))
         Cursor.lockState = CursorLockMode.Locked;


I found the answer here: link

answered by 16.08.2017 / 00:45