Creating a Rocket with Constant Force in Unity

Sharing is caring:

Use Force to Launch a Rocket

In this Unity tutorial we will be showing you how to create a simple rocket that uses the constant force physics components to launch our model rocket. We will be using Javascript for key input scripting. We will also briefly go over how to use the trail renderer to create a simple afterburner effect on our rocket.

In this video I have a simple 3d model of a rocket I created in Blender. Whatever object you want to use for this tutorial is fine as long as it has a rigidbody attached to it. Constant force will be applying upon the rigidbody of an object so this is important to have. Once you’ve added your rigidbody you’ll then add a “constant force” component.

You don’t need to make any changes to the constant force component within the inspector panel, we’ll be doing all of that within our script. Our rocket script will contain all the information for applying a force to our rocket in both the “force” and “relative torque” to our rocket model. Force applies a force to our model using world coordinates while “relative” torque applies a torque to our rocket model based off the local coordinates of the actual model.

So there is force, torque, and relative force and relative torque. Relative applies to the local coordinates of the model, while the others apply to world coordinates. We’ll also apply a sound clip to our rocket when it takes off along with a simple trail render to simulate the “rocket flames”.

We’ll test it out and see that our rocket flies straight up and rotates on the way up. Attach a camera looking downward at the top of our rocket and see how far up your rocket is flying. Scripts found below, along with the link to the audio clip used.

 

Audio Clip

http://soundbible.com/1794-Missle-Launch.html

 

Javascript Files

RocketForce.js


var force : Vector3; //variable for "lift" of the rocket

var relativeTorque : Vector3; //variable for torque (spin) of rocket

var liftSpeed : float;    //determines how much force will be applied

var turnSpeed : float; //determines how much torque will be applied

function Update () {

if (Input.GetButtonDown("Fire1")) {

constantForce.force = Vector3.up * liftSpeed; //determines the "lift" of the rocket, hence the "up" times the liftSpeed

constantForce.relativeTorque= Vector3(0,turnSpeed,0); //determines torque placed on rocket in order to turn

audio.Play(); //plays the audio clip attached to the model

}

}

 

Jonathan Gonzalez

I love all things in game development and want to contribute by teaching others how to create games. I'm always looking to help others create awesome digital worlds. I've been a hardcore gamer since I was a kid and now it's my turn to create awesome digital experiences.

More Posts - Website

Follow Me:
TwitterYouTube

Comments

  1. how to create UI button to launch rocket?

Speak Your Mind

*