- Home /
 
Move an object forward and not up?
I need to move an object in the direction its looking but only on the X and Z axis not the Y. I am currently using:
 float h = (Input.GetAxis("Horizontal") / moveScale) * sprinting;
         float v = (Input.GetAxis("Vertical") / moveScale) * sprinting;
         gameObject.transform.localPosition += transform.right * h;
         gameObject.transform.localPosition += transform.forward * v;
 
               And it works ok, but if my rotation is facing upwards i move up and i dont want that to happen, i just want to move horizontal.
               Comment
              
 
               
              Answer by HammerCar · Jun 20, 2015 at 06:50 PM
I think you can just do this:
 float h = (Input.GetAxis("Horizontal") / moveScale) * sprinting;
 float v = (Input.GetAxis("Vertical") / moveScale) * sprinting;
  
 gameObject.transform.localPosition += transform.right * h * new Vector3 (1, 0, 1);
 gameObject.transform.localPosition += transform.forward * v * new Vector3 (1, 0, 1);
 
               Here I have multiplied the y value with 0 so it is always 0. Everything else should be normal.
Your answer