- Home /
Spaceship movement
Hello,
I'm kinda new to unity and I want to make a space fighter game. I want it to exist in all three axis, meaning that I can pitch (W and S keys), roll (A and D keys) and yaw (Q and E keys). I got that down and it works (besides the part where it stops spinning, some sort of counter rotation if I don't use any buttons), but I'm struggling with the acceleration (R and F keys).
I do not want my ship to return to a standard rotation when I release all keys.
I have tried using AddForce, AddRelativeForce, transform.position and velocity, in C#, but when I use any of these, the ship doesn't move in the direction I'm facing.
For example, if I add 10 force on Start(), my ship moves forward and (since there's no drag) keeps doing so, which is supposed to happen. But as soon as I start to rotate, it keeps moving in the same direction relative to the the world axis, not relative to the space ship's axis.
Is it possible to move the ship in the direction it is facing, without applying a new force? If so, what line of code should I use? And if not, how can I fix the problem?
I want to be able to use a joystick in the future, but I should be able to do that myself.
void FixedUpdate () {
targetspeed = Mathf.Round(speed * maxspeed);
currentspeed = Mathf.Round(GetComponent<Rigidbody> () .velocity.z);
float roll = Input.GetAxis ("Horizontal"); //A en D
float pitch = Input.GetAxis ("Vertical"); //W en S
float yaw = Input.GetAxis ("Roll"); //Q en E
float throttle = Input.GetAxis ("Throttle"); //R en F
if (Input.GetKey ("r") && speed < 1f) {
}
if (Input.GetKey ("f") && speed > -0.1f) {
}
GetComponent<Rigidbody> ().AddRelativeTorque(pitch*turnspeed*Time.deltaTime, yaw*turnspeed*Time.deltaTime, -roll*turnspeed*Time.deltaTime);
It sounds like it's working fine if you want spaceship motion. If you turn off your thrusters in space then start rotating, you will still be moving in the same direction as when you had your thrusters on. Unless I'm misunderstanding you.
First, sorry for the late reaction. I've been kinda busy lately and didn't get to making games a lot. Anyway, thanks for the answers! Now that someone explaned it I realize that this is too hard to start off with. I'll just continue this project with "arcade physics" and try to make this with horizontal gameplay. I might get back to this later, when I know a bit more :)
Answer by AlwaysSunny · Apr 30, 2015 at 10:09 PM
"Is it possible to move the ship in the direction it is facing, without applying a new force?"
Nope. That would violate Newton's First Law of Motion. ;)
I think you may be confused about what you're asking for. It sounds like what you want is some kind of hybrid between "true" physics and "arcade" physics. This is what developers should want in most cases, but it requires careful consideration and a decent familiarity with the physics system and its many "gotchas".
In "arcade" style phyics, vessels have no momentum (or have high drag). Instead, they instantly set their velocities based on the desired movement direction (e.g. vessel.forward). In that case, you shouldn't be using forces to do anything. Script all your kinematics by hand. Let the physics system handle collisions, but nothing else.
In "true" physics, vessels will continue in a straight line forever until acted on by some force, like thrust. These vessels have momentum and no drag. They can rotate without changing their velocity, then apply a force to change their velocity. They require N seconds of thrust to reach a certain speed, then N seconds of force in the opposite direction to stop. In this case, you can let the physics system do nearly everything for you. All you do is apply forces.
In practice, a true-physics space sim is more aggravating than fun. And a fully "arcade" style physics sim feels clumsy and "retro". Your job is to strike some kind of satisfying balance between the two.
True forces (as opposed to scripted kinematics) can work for a true/arcade hybrid, provided you make several concessions. One key "unrealistic" change involves drag. The perfect balance between drag and thrust ( AddForce / AddTorque ) will give you a fun, responsive craft.
Don't forget about the rules and best practices associated with rigidbodies and physics. Carefully review ALL the documentation associated with physics prior to investing a ton of time on a potentially unworkable model.
Answer by BMRX · Apr 30, 2015 at 10:17 PM
So when you try to rotate/turn your ship Ex: Bank left. Your ship will rotate in that direction, yet the force is still pushing you in the same axis?
Ship is heading towards the Y axis, ship banks towards X, ship keeps moving towards Y?
Almost sounds like you've coded throttle to move only in the axis that the ship is moving, if that makes sense. You might want to try adding force not in the axis you want to go but instead the direction you want to go.
I don't know if this would work but maybe it will give you an idea:
rb.AddForce(transform.TransformDirection(Vector3.right) * throttle);
http://docs.unity3d.com/ScriptReference/Transform.html
Don't take me for an expert, I'm still learning as well.
You're correct, the OP is failing to apply force in the ship's forward direction.
body.AddForce( body.transform.forward * whatever );
Guess I forgot to mention that. He's also misunderstanding various other critical aspects of creating a stable, satisfying rigidbody controller. It is not the straightforward task it appears to be.
Nice, thanks sunny thinking this stuff through is too much fun. Hopefully OP can take your advice and get it to work.