How do I make a rocket?
I'm trying to create a rocket, where you would press the space bar, and hold it to keep going up, and you could use wasd to control which way the rocket faces. How would I create a rocket like this? I had looked at a video of how to do it with constant force, but for some reason Unity came up with an error that "constantForce" was obsolete. I found that strange especially since it was a component that I could add to the rocket itself. Any help?
Answer by Developer1212 · Nov 06, 2016 at 09:46 PM
You can use the AddForce () function instead of the constant force function.
Rigidbody.AddForce() only applies force for one frame and returns to zero. Constant force is constantly applying force and only changes when the force or torque value is changed.
Not if you use it like this:
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space)) {
rigidbody.AddForce (Vector3.something * Time.deltaTime);
}
Answer by Makri907 · Nov 06, 2016 at 10:28 PM
IMPORTANT: Be sure to add a Constant Force Component to your rocket. Create a C# script on your rocket and attach this to it:
ConstantForce thrust;
public Vector3 rocketDirection;
Vector3 copy;
public float xlrate;
void Start () {
thrust = gameObject.GetComponent<ConstantForce> ();
copy = rocketDirection;
}
void Update () {
if (xlrate >= 20f) {
xlrate += .025f;
}
if (Input.GetKey (KeyCode.Space)) {
thrust.force = rocketDirection * xlrate;
rocketDirection = copy;
}
if (Input.GetKeyUp (KeyCode.Space)) {
thrust.force = rocketDirection * xlrate;
rocketDirection = Vector3.zero;
}
}
}
Note: Set the xlrate to something reasonable like 2 or 3. And for the Y position on rocketDirection, somewhere between 10 and 20.