- Home /
Replicating the behavior of "GetAxis" but with a different input?
Hi, I'm developing a mobile game with a single "joystick" that gives me float values on the x and y axes between 1 and -1. This value is passed into my vehicle controller using an event. I have code that uses WASD controls using Input.GetAxis when using a PC and the joystick when on mobile. I want to replicate the behavior that GetAxis gives me when using my Joystick, specifically the "gravity" that causes the axis value to come back to zero. How could I go about doing that?
I really appreciate any direction, thank you!
Answer by robertbu · Oct 07, 2014 at 02:56 PM
I going to assume that "specifically the "gravity" that causes the axis value to come back to zero." is your specific question. As a starting point, you will need to keep two values, the last value returned, and the current 'raw' value. Then in each frame (not each time it is read) you do:
currentVal = Mathf.MoveTowards(rawValue, currentVal, Time.deltaTime * speed);
'speed' is a variable you define, and represents units per second. If you don't like the response from this code, replace MoveTowards() with Lerp() and decrease the value used for 'speed'. There are also other ways of adjusting the response.
Ah, brilliant. This is just what I was looking for, thanks!
Your answer