Search Unity

Keyboard Camera Movement - how to go forward/backward with a rotated transform?

Discussion in 'Scripting' started by jzaun, Apr 19, 2015.

  1. jzaun

    jzaun

    Joined:
    Feb 20, 2010
    Posts:
    26
    I have a GameObject with a camera component. I've attached the following script. I also have a Plane for a the ground.

    Everything is working as expected with the exception of the up and down arrow keys. The are working correctly as coded, that much I understand. The GO transform is rotated a bit so "transform.forward" is pointing down a bit so going forward takes you through the Plane and backward take you out of the plane.

    My question is how do I change the code so the camera moves "correctly". I would like the up and down keys to move you along the Plane same as the left and right keys do, regardless of the transforms rotation x.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. [RequireComponent (typeof (Camera))]
    6. public class WorldEditorCamera : MonoBehaviour {
    7.     public Camera cam;
    8.  
    9.     private float _zoom = 20.0f;
    10.     private float _mouseZoomSpeed = 80.0f;
    11.    
    12.     private float _orbitXSpeed = 240.0f;
    13.     private float _orbitYSpeed = 123.0f;
    14.     private int _orbitYMin = 35;
    15.     private int _orbitYMax = 85;
    16.     private float _orbitX = 22.0f;
    17.     private float _orbitY = 33.0f;
    18.  
    19.     private Transform _transform;
    20.     private Vector3 _position;
    21.     private Light _light;
    22.  
    23.     public void Start () {
    24.         _transform = transform;
    25.         _transform.localPosition = new Vector3(0f, 20f, 0f);
    26.         _position = _transform.position;
    27.  
    28.         _orbitX = 22f;
    29.         _orbitY = 33f;
    30.        
    31.         cam = gameObject.GetComponent<Camera>();
    32.  
    33.         // Make the rigid body not change rotation
    34.         if (GetComponent<Rigidbody>())
    35.             GetComponent<Rigidbody>().freezeRotation = true;
    36.     }
    37.  
    38.     public void Update () {
    39.         // Update position
    40.         if(Input.GetKey(KeyCode.RightArrow)) {
    41.             _position += _transform.right * _orbitXSpeed * Time.deltaTime;
    42.         }
    43.         if(Input.GetKey(KeyCode.LeftArrow)) {
    44.             _position -= _transform.right * _orbitXSpeed * Time.deltaTime;
    45.         }
    46.         if(Input.GetKey(KeyCode.UpArrow)) {
    47.             _position += _transform.forward * _orbitXSpeed * Time.deltaTime;
    48.         }
    49.         if(Input.GetKey(KeyCode.DownArrow)) {
    50.             _position -= _transform.forward * _orbitXSpeed * Time.deltaTime;
    51.         }
    52.  
    53.         // Update rotation
    54.         if (Input.GetKey(KeyCode.A)) {
    55.             _orbitX += _orbitXSpeed * 0.02f;
    56.         }
    57.         else if (Input.GetKey(KeyCode.D)) {
    58.             _orbitX -= _orbitXSpeed * 0.02f;
    59.         }
    60.         if (Input.GetKey(KeyCode.W)) {
    61.             _orbitY += _orbitYSpeed * 0.02f;
    62.         }
    63.         else if (Input.GetKey(KeyCode.S)) {
    64.             _orbitY -= _orbitYSpeed * 0.02f;
    65.         }
    66.         _orbitY = ClampAngle(_orbitY, _orbitYMin, _orbitYMax);
    67.  
    68.         // Update zoom
    69.         _zoom += -Input.GetAxis("Mouse ScrollWheel") * _mouseZoomSpeed * 0.02f;
    70.         _zoom = Mathf.Clamp(_zoom, 15, 100);
    71.     }
    72.    
    73.     public void LateUpdate () {
    74.         Quaternion rotation = Quaternion.Euler(_orbitY, _orbitX, 0.0f);
    75.         Vector3 position = rotation * new Vector3(0.0f, 0.0f, -_zoom) + _position;
    76.        
    77.         _transform.rotation = rotation;
    78.         _transform.position = position;
    79.     }
    80.    
    81.     public static float ClampAngle (float angle, float min, float max) {
    82.         if (angle < -360.0f)
    83.             angle += 360.0f;
    84.         if (angle > 360.0f)
    85.             angle -= 360.0f;
    86.         return Mathf.Clamp (angle, min, max);
    87.     }
    88. }
    89.  
     
  2. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    use Vector3.up instead of transform.up for a "Straight up" vector.

    Also in ClampAngle you should use modulus
    Code (CSharp):
    1. angle = angle % 360;
    2. return Mathf.Clamp (angle, min, max);
    3.  
     
  3. jzaun

    jzaun

    Joined:
    Feb 20, 2010
    Posts:
    26
    Thanks for the fix with the clamp.

    I'm looking for the "Up Arrow" to move the camera forward level with the Plane, not up/down into the air. It still has to reference the transform somehow because the player could have used the "A" or "W" keys to rotate the camera.