- Home /
Limiting Velocity when using joystick
I'm working on a 2D topdown game using 2D components. After implementing a joystick (https://www.assetstore.unity3d.com/en/#!/content/15233), I'm not able to limit my player speed.
I've tried different ways like this: http://answers.unity3d.com/questions/265810/limiting-rigidbody-speed.html . But when controlling with joystick, it simply ignores that part of the script.
I cannot see how a joystick should be different than keyboard inputs, so how can I limit the velocity?
We need to see your code that takes joystick input and applies the force. As a guess, you did not use Rigidbody2D but ins$$anonymous$$d use Rigidbody. So you need to do:
rigidbody2D.velocity = rigidbody2D.velocity.normalized * maxSpeed;
Alternate:
rigidbody2D.velocity = Vector3.Clamp$$anonymous$$agnitude(rigidbody2D.velocity, maxSpeed);
This is attachted to my player: https://github.com/$$anonymous$$umo$$anonymous$$airo/CNJoystick/blob/master/Source/CNJoystick/Scripts/CN2DController.cs
This is the joystick script. I think it's only for the joystick movement: https://github.com/$$anonymous$$umo$$anonymous$$airo/CNJoystick/blob/master/Source/CNJoystick/Scripts/CNJoystick.cs
I have changed Rigidbody to Rigidbody2D already.
The only code I could find in the provided script that does any kind of movement is in the $$anonymous$$ove() method, and it does not use a Rigidbody2D at all...and I did not find any place that $$anonymous$$ove() was called.
Your answer
Follow this Question
Related Questions
How do you get Virtual Joysticks to work? 1 Answer
How do I make a Virtual Joystick for Mobile Devices? 1 Answer
Mobile Joystick 0 Answers
Rotate GameObject Z axis only relative to Joystick 0 Answers
Adding More Torque 1 Answer