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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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;