I’m trying to use the Input.GetAxis("Horizontal")
to move a cube from left to right. The problem is, it only moves 1 unit from the starting point (which is 0). Kinda new to Unity.
Code snippet:
void Update ()
{
float posX = Input.GetAxis("Horizontal");
Vector3 paddlePos = new Vector3 (posX, transform.position.y, 0f);
this.transform.position = paddlePos;
}
Wondering why it only moves the cube 1 unit left and right. Thank you!
Input.GetAxis returns the value of the virtual axis
Currently you are setting the position of your gameobject to the return value of GetAxis instead of translating it. You should also apply deltatime to your paddle movement.
void Update ()
{
float horizontalX = Input.GetAxis("Horizontal");
Vector3 paddlePos = new Vector3 (transform.position.x + (horizontalX * Time.deltaTime) , transform.position.y, 0f);
transform.position = paddlePos;
}
or
void Update ()
{
float horizontalX = Input.GetAxis("Horizontal");
transform.Translate(0, horizontalX * Time.deltaTime, 0, Space.Self);
}