Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Clamping RotateAround()

Discussion in 'Scripting' started by Cnc96, Oct 14, 2015.

  1. Cnc96

    Cnc96

    Joined:
    Dec 17, 2013
    Posts:
    57
    Hi all,
    I know that this question has been asked many of times, but I cannot find a solution which works for my code.

    I am trying to get the camera to rotate vertically around my player object using the RotateAround function, and I also have the script rotate the player horizontally as well.
    Here is my code:
    Code (CSharp):
    1. public class MouseLook : MonoBehaviour {
    2.  
    3.     public GameObject target;
    4.     public float xSensitivity;
    5.     public float ySensitivity;
    6.     public float minAngle = 30.0f;
    7.     public float maxAngle = 80.0f;
    8.  
    9.     Transform tTransform;    //Transform reference for the camera's target
    10.     Transform mTransform;    //Transform of this object
    11.     string tName;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         tName = target.name;
    16.         tTransform = target.transform;
    17.         mTransform = GetComponent<Transform> ();
    18.  
    19.         mTransform.SetParent (tTransform);
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.         //Locking the cursor to the middle of the screen, and hiding it
    25.         Cursor.lockState = CursorLockMode.Locked;
    26.         //Cursor.visible = false;
    27.  
    28.         //Rotating the camera's target
    29.         float h = Input.GetAxis(tName + "Mouse X") * xSensitivity * Time.deltaTime;
    30.         float v = Input.GetAxis(tName + "Mouse Y") * ySensitivity * Time.deltaTime;
    31.        
    32.         tTransform.Rotate (tTransform.up * h);
    33.         mTransform.RotateAround (tTransform.position, mTransform.right, -v);
    34.     }
    35. }
    I would like to know if anybody has any idea on how I'd be able to clamp the rotation of the camera using a min and max values.
     
  2. Cnc96

    Cnc96

    Joined:
    Dec 17, 2013
    Posts:
    57
    Bump
     
  3. etig

    etig

    Joined:
    Feb 15, 2015
    Posts:
    11
    Did you find a solution?
     
  4. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    Instead of using RotateAround, set exact rotation value for the Transform using the angle. You might have to create a new Quaternion from axis angles or something, i think the error log told exactly what function to call as there was even some old way that got deprecated.