Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

translating camera along the z axis

Discussion in 'Scripting' started by gruengruen, Sep 30, 2014.

  1. gruengruen

    gruengruen

    Joined:
    Nov 7, 2013
    Posts:
    4
    Hi
    the following code sniped is attached to a camera Object:
    Code (CSharp):
    1.  
    2. var y = transform.position.y;
    3. float translateZ = Input.GetAxis ("Vertical") * CameraSpeed;
    4. transform.Translate (0, 0, translateZ * Time.deltaTime);
    5. Vector3 pos = transform.position;
    6. pos.y = y;
    7. transform.position = pos;
    8.  
    that works well for "w" and "s" to translate along the z (Space.self) axis by maintaining the Y height.
    but if the camera is rotated around the x axis ,camera movement speed is getting slower.
    how to solve this?
    thx
     
  2. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    Use another vector for movement which works on a local axis:
    Code (CSharp):
    1.  
    2. float translateZ = Input.GetAxis ("Vertical") * CameraSpeed;
    3. Vector3 movementVector = transform.forward * translateZ * Time.deltaTime;
    4. transform.Translate (movementVector);
    5.  
    (untested)
     
  3. gruengruen

    gruengruen

    Joined:
    Nov 7, 2013
    Posts:
    4
    Code (CSharp):
    1. float translateZ = Input.GetAxis ("Vertical") * CameraSpeed;
    2. Vector3 movementVector = transform.forward * translateZ * Time.deltaTime;
    3. transform.Translate (movementVector,Space.World);
    thx
    that works but i dont know why it must be Space.world
    otherwise the translation will not work along the camera local z axis