- Home /
capping the speed of a rigidbody
I am trying to write a script for a hover board.
The problem I'm having is capping the speed.
The script I have does cap the speed , but then locks out the opposite force being applied.
var moveZ : float = 0.0;
var speed : float = 1.0;
var maxSpeed : float = 5.0;
var mySpeed : float = 1.0;
function FixedUpdate () {
mySpeed = rigidbody.velocity.magnitude;
moveZ = Input.GetAxis ("Vertical");
if (mySpeed < maxSpeed)
{
rigidbody.AddRelativeForce(Vector3.forward * (moveZ * speed));
}
}
Can anyone help with this simple problem?
here is the project : http://www.alucardj.net16.net/unityquestions/hoverboard%201-1a.html
here is the full script : http://www.alucardj.net16.net/unityquestions/ScriptHover1-1a.js
(note: the textures and objects are basic =] , I am just using them to write the script , and for now the rigidbody constraints are on for rotation X and Z)
What I am looking for but cannot work out is a way to : check if speed is forward don't add more forward force (limit speed) but add normal backward force ; then if speed/movement is backwards, cap backward speed and allow normal forward force to be added . If that makes any more sense ?!
Answer by AlucardJay · Apr 15, 2012 at 08:37 AM
I have solved this using a couple of methods combined.
As the hoverboard was frictionless , I had to apply my own drag (imagined as air resistance).
Secondly, add force is now multiplied by (maxspeed - speed) / maxspeed under certain conditions ;
this is applied and calculated by normalizing the rigidbody.velocity, then using a dot product to check against if force is being added in the current direction (transform.forward); if so apply above force multiplier
e.g. Player is moving forward, input is forward , dot product is 1 , so check maxspeed , if at maxspeed addForce = 0 , else add force of (maxspeed - speed) / maxspeed while dot product > 0.5
Answer by Bicko · Mar 21, 2012 at 11:15 AM
Thanks, but what I am looking for but cannot work out is a way to : check if speed is forward don't add more forward force (limit speed) but add normal backward force ; then if speed/movement is backwards, cap backward speed and allow normal forward force to be added . If that makes any more sense ?! The link basically does what my script does now ; increase, then cap (not allowing for force to be recognized and added in opposite direction).