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. Dismiss Notice

Scene view style camera controller

Discussion in 'Scripting' started by DanteL, Dec 31, 2014.

  1. DanteL

    DanteL

    Joined:
    Dec 31, 2014
    Posts:
    9
    Hello,

    I'm looking for help trying to create a camera controller that mimics the scene view camera controls.

    The unity manual lists the scene view navigation as such:
    http://docs.unity3d.com/Manual/SceneViewNavigation.html

    Specifically I am looking for these types of controls:
    • Hold right click and mouse input to pan left, right, up, and down
    • Hold right click and hold input w, s, a, and d to fly through forward, and back, and strafe left, and right, respectively at speeds that increase the longer the input is held down
    • Double left click to focus on target and move to set distance at a smoothed rate
    • Hold alt and right click to orbit around set camera target
    • Middle mouse scroll up and down to zoom in and out
    • Hold middle mouse and mouse input to drag up, down, left and right
    I've already looked at the mouseOrbit script to get some of these controls started, but I was wondering if there was any other documentation available or if the code for the editor camera was publicly available somewhere.

    Thank you!
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    There is an asset, you would have to search which will snap your editor view to the camera view. Not sure it is dynamic, as in follow the the cam if its transform etc is altered.
     
  3. DanteL

    DanteL

    Joined:
    Dec 31, 2014
    Posts:
    9
    Do you mean ctrl + shift + f? Which sets the main camera to your current editor view?
     
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
  5. DanteL

    DanteL

    Joined:
    Dec 31, 2014
    Posts:
    9
    That is an interesting tool, but not quite what I am looking for.

    What I am trying to find is a script that will allow the player to transform the position and rotation of the camera in game in the same way that I can manipulate the scene view in the editor. So far I have found a script in the wiki that allows me to add zoom and orbit functionality to the camera object.

    Code (CSharp):
    1. public Transform target;
    2.         public Vector3 targetOffset;
    3.         public float distance = 5.0f;
    4.         public float maxDistance = 20;
    5.         public float minDistance = .6f;
    6.         public float xSpeed = 200.0f;
    7.         public float ySpeed = 200.0f;
    8.         public int yMinLimit = -80;
    9.         public int yMaxLimit = 80;
    10.         public int zoomRate = 40;
    11.         public float panSpeed = 0.3f;
    12.         public float zoomDampening = 5.0f;
    13.        
    14.         private float xDeg = 0.0f;
    15.         private float yDeg = 0.0f;
    16.         private float currentDistance;
    17.         private float desiredDistance;
    18.         private Quaternion currentRotation;
    19.         private Quaternion desiredRotation;
    20.         private Quaternion rotation;
    21.         private Vector3 position;
    22.        
    23.         void Start() { Init(); }
    24.         void OnEnable() { Init(); }
    25.        
    26.         public void Init()
    27.         {
    28.             //If there is no target, create a temporary target at 'distance' from the cameras current viewpoint
    29.             if (!target)
    30.             {
    31.                 GameObject go = new GameObject("Cam Target");
    32.                 go.transform.position = transform.position + (transform.forward * distance);
    33.                 target = go.transform;
    34.             }
    35.            
    36.             distance = Vector3.Distance(transform.position, target.position);
    37.             currentDistance = distance;
    38.             desiredDistance = distance;
    39.            
    40.             //be sure to grab the current rotations as starting points.
    41.             position = transform.position;
    42.             rotation = transform.rotation;
    43.             currentRotation = transform.rotation;
    44.             desiredRotation = transform.rotation;
    45.            
    46.             xDeg = Vector3.Angle(Vector3.right, transform.right );
    47.             yDeg = Vector3.Angle(Vector3.up, transform.up );
    48.         }
    49.        
    50.         /*
    51.      * Camera logic on LateUpdate to only update after all character movement logic has been handled.
    52.      */
    53.         void LateUpdate()
    54.         {
    55.             // If Control and Alt and Middle button? ZOOM!
    56.             if (Input.GetMouseButton(2) && Input.GetKey(KeyCode.LeftAlt) && Input.GetKey(KeyCode.LeftControl))
    57.             {
    58.                 desiredDistance -= Input.GetAxis("Mouse Y") * Time.deltaTime * zoomRate*0.125f * Mathf.Abs(desiredDistance);
    59.             }
    60.             // If middle mouse and left alt are selected? ORBIT
    61.             else if (Input.GetMouseButton(2) && Input.GetKey(KeyCode.LeftAlt))
    62.             {
    63.                 xDeg += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
    64.                 yDeg -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
    65.                
    66.                 ////////OrbitAngle
    67.                
    68.                 //Clamp the vertical axis for the orbit
    69.                 yDeg = ClampAngle(yDeg, yMinLimit, yMaxLimit);
    70.                 // set camera rotation
    71.                 desiredRotation = Quaternion.Euler(yDeg, xDeg, 0);
    72.                 currentRotation = transform.rotation;
    73.                
    74.                 rotation = Quaternion.Lerp(currentRotation, desiredRotation, Time.deltaTime * zoomDampening);
    75.                 transform.rotation = rotation;
    76.             }
    77.             // otherwise if middle mouse is selected, we pan by way of transforming the target in screenspace
    78.             else if (Input.GetMouseButton(2))
    79.             {
    80.                 //grab the rotation of the camera so we can move in a psuedo local XY space
    81.                 target.rotation = transform.rotation;
    82.                 target.Translate(Vector3.right * -Input.GetAxis("Mouse X") * panSpeed);
    83.                 target.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World);
    84.             }
    85.            
    86.             ////////Orbit Position
    87.            
    88.             // affect the desired Zoom distance if we roll the scrollwheel
    89.             desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance);
    90.             //clamp the zoom min/max
    91.             desiredDistance = Mathf.Clamp(desiredDistance, minDistance, maxDistance);
    92.             // For smoothing of the zoom, lerp distance
    93.             currentDistance = Mathf.Lerp(currentDistance, desiredDistance, Time.deltaTime * zoomDampening);
    94.            
    95.             // calculate position based on the new currentDistance
    96.             position = target.position - (rotation * Vector3.forward * currentDistance + targetOffset);
    97.             transform.position = position;
    98.         }
    99.        
    100.         private static float ClampAngle(float angle, float min, float max)
    101.         {
    102.             if (angle < -360)
    103.                 angle += 360;
    104.             if (angle > 360)
    105.                 angle -= 360;
    106.             return Mathf.Clamp(angle, min, max);
    107.         }
    108.     }
     
  6. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,680
    So, in game you want the player to be able to adjust the camera position, rotation, via a mouse controller? Sure, of course, why not?
     
  7. sawed61

    sawed61

    Joined:
    Mar 21, 2020
    Posts:
    1
    thank you so much Dantel. You saved my life
    I've been looking such a code for weeks
    thank you a lots.
     
  8. skopto

    skopto

    Joined:
    Jun 1, 2022
    Posts:
    1
    Source code references here:
    https://github.com/Unity-Technologi...bd50/Editor/Mono/SceneView/SceneViewMotion.cs