# Unity Missile Tracking

This topic is 3462 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been adding guided missiles to the game I'm working on, but have run into something of a snag. I'm basing the method on this thread: http://www.gamedev.net/community/forums/topic.asp?topic_id=419681 The code I'm using follows:
// This is the fun part -- track target
SF3dVector heretotarget = target->GetPos() - Position;
heretotarget = Normalize3dVector(heretotarget);

SF3dVector facingDifference = heretotarget - ViewDir;
float yawdirection = DotProduct(facingDifference, RightVector);
float pitchdirection = DotProduct(facingDifference, UpVector);

RotateX((yawdirection * 90)/fps);
RotateY((pitchdirection * 90)/fps);


The function DotProduct is defined as follows
float DotProduct(SF3dVector u, SF3dVector v)
{
return (u.x * v.x) + (u.y * v.y) + (u.z * v.z);
}


Normalize3dVector is defined as follows
SF3dVector Normalize3dVector( SF3dVector v)
{
SF3dVector res;
float l = GetF3dVectorLength(&v);
if (l == 0.0f) return NULL_VECTOR;
res.x = v.x / l;
res.y = v.y / l;
res.z = v.z / l;
return res;
}


When I spawn a missile object sending it off towards its target, it seems to spiral in an attempt at course correction, but ends up spiraling off into the distance at some funny angle instead. Does anyone see a problem in my code anywhere?

##### Share on other sites
At first glance, it appears as though you've mistakenly swapped rotating on the X axis for Yaw (should be Pitch) and Y for Pitch (should be Yaw).

Also, make sure you normalize your vectors before performing a dot product (to get a proper cosine of the angle between them).

##### Share on other sites
Ok, fixed the swapped x and y as well as normalized the Up and Right vectors. This seems to have helped somewhat, but not completely...

I can't quite tell whats going on... it seems that after I change targets (the first being destroyed), the missiles start acting wonky again. Perhaps they are trying to track the old destroyed enemy... but I don't see why that should be happening... relevant code below:

Constructor
Rotatable *target;    Guided_Missile_Launcher(float vel, float rt, float dmg, Rotatable *par, float dispx, float dispy, float dispz, float rx, float ry, int *a, Rotatable *targ)    {        speed = vel;        damage = dmg;        dx = dispx;        dy = dispy;        dz = dispz;        pitch = rx;        yaw = ry;        reloadtime = rt;        timelastfired = 0;        parent = par;        Position = parent->GetPos();        UpVector = parent->GetUp();        RightVector = parent->GetRight();        ViewDir = parent->GetView();        MoveForward(-dz);        StrafeRight(dx);        MoveUpward(dy);        RotateX(pitch);        RotateY(yaw);        ammo = a;        target = targ;    }

spawning the missile object
allprojectiles.push_back(new Guided_Missile(speed, damage, this, target));

Creating the weapon instance
Weapons.push_back(new Guided_Missile_Launcher(-0.95, 0.5, 10, this, 0.05, 0.05, 0.1, -1.0, 1.0, &pammo, playertarget));

where playertarget is a pointer to a rotatable object (Rotatable *playertarget)

##### Share on other sites
Did you normalize facingDifference?

##### Share on other sites
oops, just did. all that did was tighten up the turning for the case where it works... but it still misbehaves as before. Watching it carefully, on new targets it turns towards it but then continues turning in a wide arc and misses

##### Share on other sites
Just remembered to normalize ViewDir as well but that has no effect. I'm pretty certain that the weapon refuses to change targets for some reason.

##### Share on other sites
I'm pretty sure I'm passing a reference incorrectly somewhere... always have so much trouble tracking those errors down.

##### Share on other sites
It's spiraling because when it's near the target it can't turn fast enough to move towards it. You can fix it with a combination of any of the following:

1) Make the missile turn faster.
2) Make it start far away from the target, giving it time to lock on (by the time it reaches the target it should be going straight at it).
3) (And this one's really important) Make sure the collision area is not too tiny, because unless you're implementing a proper missile control system (like the ones used in real life [grin] give me a beep if you wanna implement one of those) it will never hit the target with 100% accuracy.

4) Another work-around (harder to implement) is to have a finer simulation step (ie, more FPS for the physics).

##### Share on other sites
Just tried messing with turn speed... it is definitely not the turn speed at this point (though increasing the turn speed gives the missile a much cooler looking path). I am 100% sure that the missile launcher's target is not updating properly for some reason.

##### Share on other sites
Sounds like you'll need to step thru with a debugger. The actual tracking code looks pretty good to me, without having run it myself.

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 33
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631745
• Total Posts
3002003
×