- Home /
Neutralize momentum for better turning with physics
Hello -
I am running into a somewhat conceptual problem with my space ship movement. I am using AddRelativeForce to make the ship move forward toward a target, and theoretically the ship is always facing the target. When its straight, it works just fine. However, if the target moves during this the forward momentum I am adding onto my ship with AddRelativeforce isn't always in the same direction, and I get this (physically accurate..) momentum so the ship misses the target, flying around it in an arc.
This is fine in theory, but I want to make a function that will straighten out the ship so it flies forward, without the momentum.
I am currently using a way that stores the current rigidbody.velocity in a variable (velocity), then sets rigidbody.velocity to zero, then changes rigidbody.velocity = velocity.magnitude (for the speed it was going) * transform.forward (so it takes that original speed and changes it into forward motion). This is working just fine, but I end up with an unnatural stop type thing.
I want to make a function with a name something like "StraightenOut" so it takes the velocity, and slowly over the course of 2 seconds (using Time.deltaTime I think would be best..) switches the sideways velocity to be forward velocity. It would work in the sense that each time the function is run (x number of times over the course of 2 seconds) it subtracts a little bit from the current rigidbody.velocity and then adds it back in going in the forward direction. However, I am trying to keep me speed, or rigidbody.velocity.magnitude, to be relatively the same as it changes the velocity. The problem I am running into is that since my forward additions are changing my rigidbody.velocity each frame, I'm always adding / subtracting a different number and not a percentage of the original velocity. Does anyone know a way I can structure such a "straighten out" function and make my straightening out business a bit more natural looking?
Thanks!