Accelerating rigidbody towards max speed

Discussion in 'Physics' started by PanzerPotato, Jun 17, 2018.

?

How should I move and accelerate my vehicle?

Poll closed Jun 22, 2018.

0 vote(s)
0.0%

100.0%

0 vote(s)
0.0%
4. You picked the wrong game to make...

0 vote(s)
0.0%
Not open for further replies.
1. PanzerPotato

Joined:
Oct 28, 2017
Posts:
21
Hey guys,

I'm currently in the process of creating a naval game, and I have been trying to find a way for the ships (which have rigidbodies) to accelerate or decelerate to a certain speed. Drag will affect the ships. Any ideas on how I would approach this problem?

Thanks!

(By the way, I am aiming for movement similar to that of World of Warships.)

2. PanzerPotato

Joined:
Oct 28, 2017
Posts:
21
Additionally, on Unity Answers, someone pointed out that I could use this snippet;
Code (CSharp):
1. speed = transform.InverseTransformDirection (rb.velocity).z;
2. force = Mathf.Clamp ((maxSpeed * (order / 4f) - speed) * acceleration, -maxForce, maxForce);
3. rb.AddForce (transform.forward * force * Time.deltaTime, ForceMode.Force);
It works swell, but with one major problem: It does not account for drag, resulting in the ship not being able to reach the target speed. Any ideas?

3. jtbentley

Joined:
Jun 30, 2009
Posts:
1,397
The rigidbody itself has a drag setting, which you can access via .drag (also angular drag), the effects of this won't really be noticeable until you cease acceleration.

For something interacting with water in a non-trivial manner, I would suggest clamp velocity manually by figuring out the length of the velocity vector and scaling it over time accordingly.

PanzerPotato likes this.

Joined:
Mar 3, 2024
Posts:
1
I think, to get the acceleration of a rigidbody we can simply define it like acceleration = rigidbody. mass/force
Where "force" is a variable defined in an AddForce(0, 0, force);method I hope it's understood..

Joined:
May 24, 2013
Posts:
11,700