Search Unity

Move Object Forward on Certain Axes

Discussion in 'Scripting' started by cjburkey01, Nov 29, 2015.

  1. cjburkey01

    cjburkey01

    Joined:
    Nov 8, 2013
    Posts:
    26
    I'm creating a camera, and it is facing slightly down, I would like to move the camera on the x and the z axes forward, not on the y axis. So it will move parallel to the ground forward, not towards it. If anyone needs a better explanation, please ask.
     
  2. cjburkey01

    cjburkey01

    Joined:
    Nov 8, 2013
    Posts:
    26
    I've solved it, here is my code:
    Code (csharp):
    1. Vector3 newpos = transform.position;
    2. newpos += transform.forward * movespeed * Input.GetAxis("Vertical") * Time.deltaTime;
    3. newpos += transform.right * movespeed * Input.GetAxis("Horizontal") * Time.deltaTime;
    4. newpos.y = transform.position.y;
    5. transform.position = newpos;