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?
You can use the AddForce () function instead of the constant force function.
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.