- Home /
Eight-directional movement with joystick
Hi, I've been having some trouble. I'm trying to make my movement script, but I can't figure out how to get eight-directional movement. On keyboard, everything works fine. But any time I plug in my controller, I have full 360 degree movement. Being limited to eight directions it integral to my game's design. Any help would be greatly appreciated.
Answer by JFGames · Oct 26, 2019 at 03:21 AM
I managed to find a solution, here's what I ended up with:
int QuantizeAxis(float input)
{
if (input < -0.35f) return -1;
if (input > 0.35f) return 1;
return 0;
}
if (Input.GetAxisRaw("Horizontal") > 0 || Input.GetAxisRaw("Horizontal") < 0)
{
float NewX = QuantizeAxis(Input.GetAxisRaw("Horizontal"));
Direction.x = NewX;
RB.velocity = new Vector3(NewX * Speed ,RB.velocity.y ,RB.velocity.z);
}
if (Input.GetAxisRaw("Vertical") > 0 || Input.GetAxisRaw("Vertical") < 0)
{
float NewY = QuantizeAxis(Input.GetAxisRaw("Vertical"));
Direction.y = NewY;
RB.velocity = new Vector3(RB.velocity.x ,RB.velocity.y ,NewY * Speed);
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Controller Joystick Hold Delay Logic? 1 Answer
How to get 8 direction Movement with controller sticks | Topdown 3D 1 Answer
Basic character movement with joystick 1 Answer
How do I listen for Input from joystick/buttons on a controller? 1 Answer