- Home /
Quick and snappy movement
In my game precision and speed is important. I have this script to control the player:
private float Dir;
private Vector3 neg = new Vector3(-1, 0, 0);
private Vector3 pos = new Vector3(1, 0, 0);
void Update ()
{
Dir = Input.GetAxis ("Horizontal");
if (Input.GetButtonDown ("Horizontal"))
{
if (Dir < 0)
{
transform.position = transform.position + neg;
}
else if(Dir > 0)
{
transform.position = transform.position + pos;
}
}
}
The script works, but i find that it takes too long after the button is pressed for the player to actually move, this means that you cant quickly double tap on the button, you instead have to wait just a little bit before you can press the button again.
This makes the game unplayable at higher speeds, any ideas how to speed it up?
Answer by BlackHack_ · Aug 29, 2017 at 04:40 PM
Unity does some smoothing to Input.GetAxis() witch makes it reach the target number in about 0.5 seconds, and because you check whether its bigger than zero or less that zero then this condition will be true for about 0.5 seconds after the user stops pressing the button, you can avoid the smoothing by using Input.GetAxisRaw(), or you can use it for your advantage:
transform.position += new Vector3(Dir, 0, 0);
If you are trying to preserver performance by cashing the Vector3, then I'll tell you that it doesn't matter, it matters when you are dealing with a class (reference types), but Vector3 is a struct witch means it doesn't allocate on the heap.
Thanks @BlackHack_ ! That worked:) I'm still pretty new at this so thanks for the help, didn't think anyone were going to respond.
Your answer
![](https://koobas.hobune.stream/wayback/20220612133744im_/https://answers.unity.com/themes/thub/images/avi.jpg)