Monstr92
Monstr92

Reputation: 404

Unity How to make GameObject Speed gain velocity

I have a Torpedo in a Unity3D game that I'm making in Unity3D and I have a Torpedo fire out of a sub. How can I make the Torpedo fire like a Torpedo (start really slow) and then gain lots of momentum and speed up, like in movies.

Below is my code for how I'm doing this, but it doesn't work very well.

    float torpedoSpeed = (0.00001f) * 155.2f;

   //move
    gameObject.transform.position += new Vector3(velocity, 0, 0) * 15.5f;

Upvotes: 2

Views: 4622

Answers (1)

axwcode
axwcode

Reputation: 7824

Modern torpedo can actually speed up because they are propelled. Therefore the trick is to accelerate the torpedo.

Acceleration requires a Force in a direction. You must first determine the mass of the torpedo, which will allow you to apply a Force to it so that it accelerates.

enter image description here

So acceleration is the Force applied divided by the mass of the object.


That being said, you can add force to an object in Unity by simply using:

gameObject.rigidbody.mass = 0.5;

gameObject.rigidbody.AddForce(100, 0, 0);

Or you can add a constant force that will keep the torpedo always accelerating.

gameObject.constantForce.relativeForce = Vector3(0, 0, 1);

Upvotes: 6

Related Questions