- Home /
Best way to use Rigidbody.AddForce to fly straight only?
I had no idea how to title that.
Basically, I have a flyer in a 3D environment. In order to take advantage of rigidbody drag and not mess with the physics, I'd like to use AddForce to do movement, since doing so affects the velocity, which is affect by the drag.
However, if I fly straight so my velocity gets up to (0,0,10), and I want to go up, it obviously doesn't continue on my local Z-axis as I turn, it continues pushing me globally on the Z-axis and new force simply adds to the global Y-axis.
Is it possible to move as if I have an engine in the back pushing me, so as I turn my velocity continues in that direction, constantly on my local Z-axis?
Thanks!
Answer by robertbu · May 17, 2013 at 11:09 PM
You want to add force in the direction of the front of your plane. So you want to use Transform.forward. Something like:
rigidbody.AddForce(transform.forward * force * Time.deltaTime);
This way, your plane will be forced forward in the direction it is looking.
No, you miss the point. That adds force to the rigidbody in said direction, but it's WORLD space total. So if I wanted to turn and have it continue forward in my LOCAL forward, it wouldn't, because the force applied would continue pushing me forward in WORLD space.
That's my dilemma.
Vector3.forward is world forward. Transform.forward is the forward of the game object. So if you turn the plane, any new force will be applied in the new forward direction of the plane (not the world forward). Note that there will still be velocity in the old forward direction that will need to decay as would happen with a real plane or spaceship. Upping the drag will cause it to decay faster. If you really want an instant turn (and can live with the unrealistic physics), you can assign the velocity directly in Update():
rigidbody.velocity = transform.forward * speed;
That was the point of the post was that I wanted to know if there was a way to turn the velocity of the ship with the rotational changes. If I turn on the drag too high than the velocity in any direction will decay too quickly. I just don't like a control scheme where you want to go up but are still going a different direction, even though that's technically vacuum physics.
Likely just have to do it some shitty way.
I've never tried it, but in theory could could use Vector3.Project().
After the turn do something like:
rigidbody.velocity = Vector3.Project(rigidbody.velocity, transform.forward);
Or another untested idea:
rigidbody.velocity = transform.forward * rigidbody.velocity.magnitude;
Answer by canadialad · Aug 23, 2014 at 03:42 AM
Would this not work:
rigidbody.AddRelativeForce(force * Time.deltatime)
From this wiki link, I would say it answers
However, if I fly straight so my velocity gets up to (0,0,10), and I want to go up, it obviously doesn't continue on my local Z-axis as I turn, it continues pushing me globally on the Z-axis and new force simply adds to the global Y-axis.
Answer by Siloaman · Jun 25, 2015 at 06:25 AM
I had this same problem concerning flying forward in World Space.
You have to use Transform.TransformDirection function.