Search Unity

Camera Help

Discussion in 'Scripting' started by Faestus, Jul 29, 2014.

  1. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    I want to make the camera rotate around an object. This is the code ATM.

    Code (CSharp):
    1. void Update ()
    2.     {
    3.         if (Input.GetMouseButtonDown (0)) {
    4.                         MouseDown = true;
    5.                 }
    6.         if (Input.GetMouseButtonUp (0)) {
    7.             MouseDown = false;
    8.         }
    9.         if (MouseDown==true)
    10.         {
    11.             if (axes == RotationAxes.MouseXAndY)
    12.             {
    13.                  rotationX = transform.localEulerAngles.y - Input.GetAxis("Mouse X") * sensitivityX;
    14.                
    15.                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    16.                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    17.  
    18.                 transform.localEulerAngles = new Vector3(rotationY, rotationX, 0);
    19.  
    20.                
    21.  
    22.             }
    23.             else if (axes == RotationAxes.MouseX)
    24.             {
    25.                 transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
    26.             }
    27.             else
    28.             {
    29.                 rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    30.                 rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    31.                
    32.                 transform.localEulerAngles = new Vector3(rotationY, transform.localEulerAngles.y, 0);
    33.             }  
    34.  
    35.             float X = target.transform.position.x+Mathf.Cos(rotationX)*CameraDistance;
    36.             float Z = target.transform.position.z+Mathf.Sin(rotationX)*CameraDistance;
    37.             float Y = target.transform.position.y+Mathf.Sin(rotationY)*Z;
    38.            
    39.             this.gameObject.transform.position = new Vector3(X,Y,Z);
    40.  
    41.         }
    42.     }

    For some reason, it twitches weird when I move the camera. Why? Can somebody help me?
     
  2. Faestus

    Faestus

    Joined:
    Jul 23, 2014
    Posts:
    68
    EDIT:

    Solved.

    Apparently, Math.Sin requires Radians, not degrees!