# Unity 3D – Constant Force

Learn how to use the Unity 3D component Constant speed and how it differs from rigidbody addForce. Edithson Will also teach you ways to optimize your code and follow up with using vectors to move the ship/bomber around.

What you'll learn
* How to use the component constant force.
* The benefits of using addForce and constantForce and when you should use it.
* Use vectors to apply force
* Intro on how to use

Keep up with more interactive tips and tricks and more on game development on Twitter with @eabelard or Add Edithson to your circle on Google plus. Be sure scroll down bellow to get the source code.

## Unity 3D Javascript Source Code

You've watch the video now its time to try out the code. To help you get startedcourtesy of Passion 47 and Edithson Abelard we have included the final unity 3d source code for you to try out yourself or just copy the code bellow and make modifications. Enjoy.

``````#pragma strict

var speed:int = 0;
var vol:float = 0;
var speedDelay:float = 0;

var posOrg:Transform;
var shipSpeed:float = 2;
var timeFactor:float = 1;
var dis:float=0;

private var speedType = "con";
private var dir = "up";

function Start () {

}

function Update () {

//checkFire();
//checkSpeed();

//	checkForce();

/* if(Input.GetKey(KeyCode.Space) &&  Time.time > speedDelay ){

switch(speedType){
case  "fire":
checkFire();
break;

case  "con":
checkCon();
break;

}

}*/

//thrust
if(Input.GetKey(KeyCode.UpArrow)  ){
constantForce.force = Vector3.up * shipSpeed;
}else if(Input.GetKey(KeyCode.DownArrow) ){
constantForce.force = Vector3.down * shipSpeed;
}

//rotate
if(Input.GetKey(KeyCode.LeftArrow)  ){
//constantForce.force = Vector3.up * shipSpeed;
}else if(Input.GetKey(KeyCode.RightArrow) ){
//constantForce.force = Vector3.down * shipSpeed;
}

}

function checkCon(){

if(dir == "up"){
Debug.Log("Current Dir: " + dir);

constantForce.force = Vector3.up * shipSpeed;
dir = "down";
}else if( dir == "down") {
Debug.Log("Current Dir: " + dir);

constantForce.force = Vector3.down * shipSpeed;
dir = "up";
}

}

function checkFire(){
if(Input.GetKey(KeyCode.Space) && Time.time > speedDelay ){
speedDelay = Time.time + timeFactor;
gameObject.transform.Translate(Vector3.up * shipSpeed);
}
}

function checkSpeed(){
//Use vector3 (X,Y,Z) to calculate the distance between the 2 positions
//The speed formula is distance/ time like 40miles / 1hour
var v3:Vector3 = new Vector3();
dis = v3.Distance(posOrg.position, transform.position);

// var dist = Vector3.Distance(posOrg.position, transform.position);
speed = dis / timeFactor;
}

function checkForce(){
if(Input.GetKey(KeyCode.Space)){
shipSpeed = 10;
speedDelay = Time.time + timeFactor;