- Home /
Moving a spaceship
Hello guys
I'm trying to make a small ship game, where the ship floats a bit above the ground, and of course you can move it forward/backward using the arrow keys, and also turn left/right. Think of the ships in WipeOut.
Since I'm new with Unity and I want to test stuff first, what I did was create a cube, set it as a rigidbody, then added a box for collision. All is fine and the object collides with the track mesh.
My question is, how do I apply movement to the ship?
In my code so far I have something like this:
function FixedUpdate () {
forward = Input.GetAxis("Vertical");
//Debug.Log ("Moving forward"+forward+" with deltatime="+Time.deltaTime);
if (forward)
rigidbody.AddForce(Vector3.forward * 5000 * forward * Time.deltaTime);
}
This seems to push the ship forward, although irregularly (it's more like it's going over a rocky terrain rather then a plain terrain) and of course it does not use the "ship's"(cube's) forward vector (Z axis). Once it collides and the cube rotates, pressing Arrow up does not move the cube forward but sideways. Do I or how do I specify that it should use the ship's Z axis always, and to make the movement constant over a period of time (say 10 meters/second). Do I use Time.deltaTime or something else since I use FixedUpdate and not Update?
Answer by Persona · Oct 24, 2011 at 08:52 PM
Use the rigidbody to disable the rotations that you don't want. Fixed Update updates itself far less than Update, so it will be rocky. Move it to Update().
No, the optimal solution is to have your physics code inside FixedUpdate(), and logic inside Update().
Answer by Kingslash · Nov 04, 2011 at 07:31 PM
You could also try using AddRelativeForce instead of AddForce. It might be that your forces are being applied based on world instead of local coordinates.
Your answer
Follow this Question
Related Questions
Can't move player with rigidbody.velocity 1 Answer
AddTorque doesn't rotate? 3 Answers
How do i get the cube to turn when up and right or left arrow is pressed? 1 Answer
Jump Input Sensitivity 0 Answers
Add force in direction to camera 2 Answers