- Home /
How to Gain Forward Speed and Momentum with a Rigidbody?
Hey guys,
I was wondering if somebody out there might be able to help me out a bit.
Basically, I have a rigidbody that is dropping from the air. When I put it into a dive, I want it to speed up, gain momentum and gain forward speed.
When I take it out of the dive, I want the rigidbody to slow down and lose forward speed/momentum.
I'm not really sure how to go about doing this.
Right now I'm just using:
rigidbody.AddForce(transform.forward*forward);
to move the rigidbody forward.
I think if you can get the forward var to increase and decrease value based on which way the rigidbody is pointing, might be a good start.
Not really sure though.
Any ideas, or help would be really appreciated.
Thanks in advance!
How about Component>Physics>ConstantForce, and comment the AddForce when trying that out
Answer by DESTRUKTORR · Aug 30, 2013 at 04:32 AM
public float diveRate = 1.0f;
public float minDiveSpeed = 10.0f;
public float maxDiveSpeed = 15.0f;
float divePos = 0.0f;
void FixedUpdate()
{
if(Input.IsButtonDown("Dive Button"))
{
divePos = Mathf.Clamp01(divePos + diveRate*Time.fixedDeltaTime);
}
else
{
divePos = Mathf.Clamp01(divePos - diveRate*Time.fixedDeltaTime);
}
rigidbody.velocity = new Vector3(rigidbody.velocity.x, -Mathf.Lerp(minDiveSpeed, maxDiveSpeed, divePos), rigidbody.velocity.z);
}
That should work :D
[Edit] If you want to change the rate of diving, simply alter "dive rate" and it will cause you to go into a dive faster or slower, respectively. As well, changing the "minDiveSpeed" and "maxDiveSpeed" will alter how fast you dive.
I used velocity because it would give you a more exact, and easily-calculated speed. However, if you want other things to alter the speeds, as well, you may want to use AddForce, and just calculate how much force is going to be applied, and how long it will last before it will need to be applied, again.