- Home /
Question by
Wanderevil · Mar 04, 2018 at 12:58 PM ·
movementvector3input.getaxis
Moving a cube on the X-axis only moves by 1 unit?
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!
Comment
Best Answer
Answer by GenericToast · Mar 04, 2018 at 01:25 PM
Input.GetAxis returns the value of the virtual axis https://docs.unity3d.com/ScriptReference/Input.GetAxis.html
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);
}
Thanks man! I feel dumb for passing the value of Input.GetAxis lol. I also multiplied it by a float value of 10f to give it a bit of speed. Cheers!