- Home /
Smooth transition for GetAxis, when pressing opposite direction
I am currently working on a space game (sideways look, scrolling top down). I am using Input.GetAxis("Horizontal"), for input to controlling multiple behaviors for a space rocket. GetAxis seems smooths from zero to 'some value' when pressing a key, and from this value back to zero as it is released. One of the things I am using the result of GetAxis to, is handling an EulerRotation of the space ship.
It works decently most of the time, but I am not satisfied with the result of pressing one key for one direction, and then pressing the one for the opposite directions, as this resets the GetAxis value to zero at the occurance. The result of this is attempted illustrated in the added figure below.
Jumpy angle shift. This is an issue as I would like to have some very polished mechanics.
I am looking for a way to disable the result of pressing 'd' whilst the rocket is tilted counter clockwise (in context of the example). Either that, or some interpolation for the values, so that 'smoothness' is preserved. Any input on this, methods or direct solutions, are appreciated.
Thanks in advance.
P.S. I program in C#, but understand JS easily.
Answer by Loius · Nov 02, 2012 at 05:02 PM
in edit > project settings > input, disable the 'snap' checkbox on the axis in question
Thanks a bunch, this results in the wanted behaviour. I am however still interested in a solution that takes to interpolating a current value towards the new one. If you could enlighten me on such a method as well I would be very much obliged.
Usually I do that by storing an 'actual' rotation and a 'target' rotation, and use an acceleration to move towards the target rotation.
Something like:
var actual : float; var target : float; var accel : float = 25;
function Update() {
actual += accel * $$anonymous$$athf.Sign(target-actual) * Time.deltaTime;
transform.rotation = Quaternion.Euler( 0,actual,0 );
target = 45 * Input.GetAxis("Horizontal");
}
But I believe the 'gravity' setting (in the Input manager also) can effect the same... effect, but automatically, if you don't need fine control over it. Gravity makes the axis's return value move slowly from point to point ins$$anonymous$$d of instantly being what the player has input.
This is exactly what I needed! I can't believe it was a built in option, I've been worrying about trying to script a solution to this all this time!
Thanks to Sonaten for asking the question and Loius for answering!
Your answer
Follow this Question
Related Questions
Smooth camera size interpolation 1 Answer
Tracking slow mouse movements 1 Answer
How do I get a value from Input.GetAxis() ? 1 Answer