- Home /
How do i make it more horizontally with no rotation?
So i am making a "car" game and i want the car to only move forward and back, and left and right, without rotation. I want it to just translate into its next position. I already have the car moving forward and back, but if anyone could add on to this and make it also move left and right it would be huge. Thanks
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class MoveForwards : MonoBehaviour { public float Speed = 60.0f;
// Update is called once per frame
void Update () {
float translation = Input.GetAxis ("Vertical") * Speed;
translation *= Time.deltaTime;
transform.Translate (0, 0, translation);
}
}
Answer by IndievdB · Sep 17, 2017 at 10:04 PM
You are getting "Up" and "Down" input with Input.GetAxis ("Vertical"). If you want "Left" and "Right" input, you will need to use Input.GetAxis ("Horizontal"). Possible code could look something like this:
void Update ()
{
// Get input of the Left and Right arrow keys.
float horizontalInput = Input.GetAxis ("Horizontal");
// Get input of the Up and Down arrow keys.
float verticalInput = Input.GetAxis ("Vertical");
// input.x will dictate how far to move in the X (sideways) dimension
// input.y will dictate how far to move in the Y (upwards) dimension
// input.z will dictate how far to move in the Z (forward) dimension
Vector3 input = new Vector3 (horizontalInput, 0, verticalInput);
// Multiply by Time.deltaTime to adjust for framerate
transform.Translate (input * Speed * Time.deltaTime);
}
Remember that transform.Translate pushes the object in 3 directions. Translate (0,0,1) would move forward by 1, Translate (0,1,0) would move up by 1, and Translate (1,0,0) would move sideways by 1.
Your answer
Follow this Question
Related Questions
left/right rotation force 0 Answers
moving like a snake to the right and to the left 1 Answer
Rigidbody2d Moving up and Down but not left and right 1 Answer
left and right movement for ai 1 Answer