0
\$\begingroup\$

I had a problem playing with the Input Manager, where for example:

When Horizontal (+) is pressed, it increase its value but, when Horizontal (-) is pressed, the horizontal value stays positive for a little, but keeps decreasing. This causes the character to continue playing "right" animations and still moving to right for a few seconds, instead of instantly playing 'left" animations and moving to left. The problem also makes the character movie a little after the player releases the key.

I have also used Input.GetKey(), but that means the player cannot change the setting from the game launcher, and I don't want to create player setting in-game, for now.

What I am trying to achieve is when Horizontal (+) is pressed, then the player presses Horizontal (-), it should instantly turn left without delay. When the player is not pressing any Horizontal key, it should stop moving horizontally, instantly. This problem also appears along the Vertical axis.

How do I make it so input snaps to -1 or 1, with out slowly moving back and forth?

\$\endgroup\$
3
  • \$\begingroup\$ Safe to assume you've read up on using the Input Manager parameters, including Snap? For context, it would help to share the current parameters you're using on each axis. \$\endgroup\$
    – DMGregory
    Dec 9 '16 at 17:13
  • \$\begingroup\$ Without the code, it's almost impossible to help with this problem. There could be any number of things that's causing this. \$\endgroup\$ Dec 9 '16 at 21:57
  • \$\begingroup\$ @JohnHamilton, it sounds exactly like the in-house input smoothing, which requires very little code to fix, and no code what so ever to identify. \$\endgroup\$
    – Gnemlock
    Dec 9 '16 at 22:23
3
\$\begingroup\$

The problem your having is that your directly accessing input; However, the input is set up as an Axis, and is intended for "analog" style controls. Consider this; if you use the buttons a and d, there is logically only a -1 and a +1. If you were to use an analog controller, the inputs would range between -1 and +1.

As such, there is a level of smoothing that is applied to the input. You could play around with this smoothing for the desired effect, but given you wish to remove the feature entirely, there is a much easier option; using Input.GetAxisRaw() instead of Input.GetAxis().


Let's take a look at the API reference:

Input.GetAxisRaw

public static float GetAxisRaw(string axisName);


Description

Returns the value of the virtual axis identified by axisName with no smoothing filtering applied.

The value will be in the range -1...1 for keyboard and joystick input. Since input is not smoothed, keyboard input will always be either -1, 0 or 1. This is useful if you want to do all smoothing of keyboard input processing yourself.


There you have it. Simply calling Input.GetAxisRaw("Horizontal") and Input.GetAxisRaw("Vertical") will retrieve the true values (-1, 0 or +1) of the desired input, without any applied smoothing.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.