- Home /
Spaceship physics - adding resistance
Hi,
I don't know enough about PhysX (or actual physics!) so was wondering if someone could help me out.
I'm trying to create some simple spaceship controls on a rigidBody. I am doing this by using AddForce to add a direction vector and move the ship accordingly. The problem is (rather obviously), once I have added the force, it stays in effect endlessly. I obviously need to add some kind of resistance to the ship so that after I move in a direction, it slowly comes to a stop (which is specified by the resistance variable or whatever.)
Firstly, is using AddForce the wrong thing to do? Secondly, can anyone suggest a method in which I could apply resistance to my equation:
var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
if (directionVector != Vector3.zero) { var directionLength = directionVector.magnitude; directionVector = directionVector / directionLength;
directionLength = Mathf.Min(1, directionLength);
directionLength = directionLength * directionLength;
directionVector = directionVector * directionLength;
}
gameObject.rigidbody.AddForce(directionVector);
Answer by Proclyon · Nov 04, 2010 at 02:23 PM
Every force has an equally strong reaction. Action is reaction.
If you hit something from left to right, you can hit it from right to left aswell using a timer or anything else depending on your desired result.
If you are in space and hit something, you will not see the real reactions in games. So just cheat and make a % of the force added to be added on opposite over a time period.
Velocity -= (Decelleration + Time.deltaTime);
Where abouts would velocity come into play in the code sample I posted? I'm a little bit confused how I'd work that in.
Assu$$anonymous$$g your code above runs only when for example you're pressing a key, then when you're not pressing a key, drag will slowly reduce the velocity until you stop. I use similar code for my spaceship game, using Force$$anonymous$$ode.Acceleration as the type of force added.
I was giving you an explanation in physics. You are going to have an object and it is going to have a speed ($$anonymous$$a.) Velocity, which is distance over time.
You can measure it or just extract it from the rigidbody.
To do that just get the RB out of object with if (GetComponent() != null) { RigidBody myRB = GetComponent(); }
private float velocity = myRB.velocity;
or in your sample above, Debug.Log(gameObject.rigidbody.velocity); (or something like that)
Answer by Atnas1010 · Nov 04, 2010 at 02:12 PM
Try setting the drag to something like 1, and just play around with it.
Angular drag is for slowing rotation speeds.
In actual space, you wouldn't slow down, because there is nothing to slow you down. But that might make a pretty crazy game.
And using AddForce is the right thing to do (if you get the desired result)