- Home /
Input System: How to limit a Vector2 input to be only up, down, left or right.
I have an Action in the new input system that has 2 bindings, the arrow keys and Gamepad DPad. I want to limit the Vector2 to be on only the 4 cardinal directions (up, down, left, right) and if 2 arrows are pressed or diagonal DPad input is detected ignore one or use the last one detected.
Answer by gev-alex · Nov 26, 2020 at 06:35 PM
If i got it right, you could use variables e.g. vertical , horizontal if (vertical) if (Input.GetKeyDown(KeyCode.W)) { vertical = true; horizontal =false; } if (horizontal) { if (Input.GetKeyDown(KeyCode.S)) vertical = false; horizontal = true; }
Answer by Envans · Nov 26, 2020 at 06:46 PM
You will have to handle that in script like when you get the Vector 2 check the value and if the values are between 1 and 0 set the to either 1 or zero depending on how close they are to either value and if both x and y axis are used you should use some clever way to decide which axis of the 2 to use. for example if axis.x = 0.7f and axis.y = 0.2f you would most likely want to use the axis.x and set it to 1, or in direction the player would move right. I don't think there is a way to do what you want in the input actions.
Your answer
Follow this Question
Related Questions
Implementing Jump w/ New Input System 4 Answers
Deriving and angle from two points 1 Answer
Need help with Vector3s and Vector2s 2 Answers
Overload Vector3's operator + 2 Answers
Joystick to turn object 1 Answer