Search Unity

Relative orbit manipulation help

Discussion in 'Scripting' started by Dahlvash, Mar 29, 2015.

  1. Dahlvash

    Dahlvash

    Joined:
    Feb 9, 2014
    Posts:
    54
    Hi.
    I have a set-up by where a camera needs to 'orbit' a game object based on user input (WASD and dragging).
    The issue I am having is that when I rotate the camera lets say 45 defrees around Y and then rotate N degrees around X, the rotation along X is not perfectly 'down' the screen. What i mean is the rotation is off kilter.

    What I'd like to happen is if the camera is moved then the camera orbits exactly around the target relative to the cameras orientation so that the illusion that the object is being scrolled perfectly up or down / left or right.

    I have looked through other posts on the forums and none seem to address this specific implementation.

    Any help would be greatly appreciated.

    current code:
    Code (CSharp):
    1.  
    2.  
    3.     public void LateUpdate()
    4.     {
    5.         if (target)
    6.         {
    7.             if (Input.GetKey(KeyCode.Mouse0))
    8.             {
    9.                 x -= -Input.GetAxis("Mouse X") * dragSpeed;
    10.                 y += -Input.GetAxis("Mouse Y") * dragSpeed;
    11.             }
    12.  
    13.             if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.A) ||Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.D))
    14.             {
    15.                 x -= Input.GetAxis("Horizontal") * xSpeed * 0.02f;
    16.                 y += Input.GetAxis("Vertical") * ySpeed * 0.02f;
    17.  
    18.                 y = ClampAngle(y, yMinLimit, yMaxLimit);
    19.  
    20.                 //distance -= Input.GetAxis("Mouse ScrollWheel") * zoomSpd * 0.02f;
    21.                 distance -= Input.GetAxis("Mouse ScrollWheel") * zoomSpd;// *0.02f;
    22.             }
    23.  
    24.             Quaternion rotation = Quaternion.Euler(y, x, 0.0f);
    25.             Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
    26.  
    27.             transform.rotation = rotation;
    28.             transform.position = position;
    29.         }
    30.     }
    31.  
    32.     public static float ClampAngle(float angle, float min, float max)
    33.     {
    34.         if (angle < -360.0f)
    35.             angle += 360.0f;
    36.         if (angle > 360.0f)
    37.             angle -= 360.0f;
    38.         return Mathf.Clamp(angle, min, max);
    39.     }