- Home /
How to limit only the Rigidbody.velocity from player input?
Hi all. Been stuck solving this problem for a week now, and thought I'd try you guys.
Basicallly I want to create a rigidbody controller that responds to both player input and forces from other rigidbodies (ie. from player movement, and collision with other gameobjects, speeding the player up).
I've tried several methods, all which were successful in limiting the player velocity. Examples of them were clamping the velocity to maxSpeed, and adding a force opposite to the force added, if the player exceeds maxSpeed.
The problem with these methods is that they limit maxSpeed from player input and also from external forces. So when another (fast-moving) rigidbody collides with the player, the player doesn't "fly off" as fast because of the maxSpeed limiting the player velocity. I'm looking for a way to limit only the velocity caused by player input, and leave external forces be. Is there a way to do this?
Any help is appreciated. Thank you!
Answer by tormentoarmagedoom · May 17, 2019 at 08:49 AM
Hello!
yes, its very simply!
What you really want, is NOT to limit the speed of the rigidboy, what you really want i only disable "addForce" from key Input detection if rigidbody speed reached a limit!
So you only need to detect the player input keyboard if the speed of the rigidbody is lower than the speed limit. So, do not limit the spped, just disable the Input detection if reached the limit speed.
In addition, also add a limit, (much more highr than limit disabling input), i mean, for example, input is disabled at 50 km/h and max speed limit is 200 km/h. (Unity can do strange things at very high speed moving objects.. :D
Bye!
Your answer
Follow this Question
Related Questions
VR Sword Physics, how to add force or velocity to the sword for impact when it hits an object 1 Answer
Rigidbody physics; forces doesnt move gameobject in a desired way 1 Answer
Do rigid bodies with force add force to other objects? 0 Answers
Setting a RigidBody's velocity messes with my custom gravity, not sure how to proceed. 0 Answers
How would you code a Wind script? 1 Answer