- Home /
Missile Homing and correcting the "dead zone"
I have a similar problem to this: http://gamedev.stackexchange.com/questions/17313/how-to-prevent-homing-entities-from-orbiting-their-targets
But i am unsure how to implement those, most game is see is where they slow down the moving object to increase its steering potential (decreasing the shadow region where it cant go to due to steer limits).
How would i detect it entering this orbit and then just decrease its velocity? (i am using the nonphysics approach for homing and using LookAt method.
Any ideas?
Answer by aldonaletto · Jan 24, 2012 at 11:05 AM
When the missile is orbiting a target, the angle between its current velocity and the vector missile->target is near to 90 degrees - the problem here is how to get the missile velocity vector, since you're not using physics. If you don't know the velocity vector, it can be calculated in Update:
var target: Transform; // the target transform private var lastPos: Vector3; // used to calculate the velocity vector
function Start(){ lastPos = transform.position; }
function Update(){ // calculate the velocity vector: var vel: Vector3 = (transform.position - lastPos)/Time.deltaTime; lastPos = transform.position; // update lastPos // get the missile->target vector: var tgtDir: Vector3 = target.position - transform.position; var angle = Vector3.Angle(vel, tgtDir); if (angle > 70 && angle < 110){ // may be orbiting target: reduce // the missile velocity } }
Why use lastpos only in start? What if I am facing target, and then missile orbits parallel to this vector? Then it doesn't work. Can't I use a deltaposition? (store position ever update iteration)? I do like your answer, but i don't really know for sure, is there some way to figure out if target is within the "shadow region" somehow? Any way to use cross product to deter$$anonymous$$e missile perpendicular vector, and see if this vector seems to always point to target? How would I see if it is doing that?
The name lastpos
should be obvious always the last position. It's more a prove of concept here. You have to set the last position after you moved your missile. Use either LateUpdate or do it after you moved it in your missile code.
$$anonymous$$ea culpa! I forgot to update lastPos in the code (I fixed my answer).
lastPos is used to calculate the delta position between updates; it's being divided by deltaTime to get the velocity, but this division can be omitted - only the direction matters.
$$anonymous$$y first idea was to use the dot product, but Angle is easier and more intuitive (both vectors should be normalized to get a consistent dot product).
The idea is to reduce the missile velocity when it is passing by the target, what could create an spiral. If you want a better behaviour, narrow the angles (>60 and < 80, for instance).
Yup I got it after it was mentioned above. I just thought I missed something. This seems to work great, I assumed it would reduce the speed in odd circumstances, but it didn't, thanks for the help! I'll use this on other parts of game code too :)