- Home /
Heat-seeking missile code.
Hey! So, I'm implementing heat-seeking missiles in a platformer game. Basically, I'm attempting to code a kind of inertial movement where the missile goes after an object (in this case, a helicopter) in the most efficient way possible. But that route will be limited by its turning radius--it won't be able to turn fast enough if the helicopter "dodges" it by moving out of the way.
In any case, my strategy is to constantly adjust the angle towards the helicopter, and then just push the copter in the direction of a vector I'm constantly changing based on that rotation. I'm having trouble with the second part of that process. It seems to be doing something right, at times--because the missile generally will go in the direction of the helicopter. Any idea what I'm doing wrong, here? Let's assume, for the purposes of sanity, that the missile always starts at angle 0.
void Update ()
{
AdjustAngle();
transform.position += directionVector; // updated in adjustangle
}
public void AdjustAngle()
{
//aim for the target. move an increment toward the target if it's not aiming at it within a certain range.
//move in the direction the missile is facing.
angleOfChopper = GetAtan2Deg();
maxMoveDistance = missileSpeed * Time.deltaTime;
amountToRotate = missileTurnRadius * Time.deltaTime;
if(angleOfChopper < transform.eulerAngles.z)
{
transform.Rotate(0,0, -amountToRotate);
directionVector = new Vector3( -1* maxMoveDistance * Mathf.Cos(amountToRotate), maxMoveDistance * Mathf.Sin(amountToRotate) , 0);
}
if(angleOfChopper >= transform.eulerAngles.z)
{
transform.Rotate(0,0, amountToRotate);
directionVector = new Vector3( maxMoveDistance * Mathf.Cos(amountToRotate), -3 * maxMoveDistance * Mathf.Sin(amountToRotate) , 0);
}
}
public float GetAtan2Deg()
{
float yDist = gameObject.transform.position.y - chopper.transform.position.y;
float xDist = gameObject.transform.position.x - chopper.transform.position.x;
return Mathf.Rad2Deg * Mathf.Atan2(yDist, xDist);
}
}
Answer by senad · Apr 20, 2012 at 06:42 AM
The logic in MoveForward() seems too complex to me.
Would it not be much easier to keep a direction vector which you rotate with the missile? When moving forward, you just add it to the missile position. That would be only one line of code for the missile movement.
Hmm...I know what you mean, but how do I deter$$anonymous$$e what the vector will be adjusted by?
Ok, so I've updated my code based on what I think you're saying I should do...but it still appears to be brokentown. Any idea what I'm doing wrong?
Is it the same wrong behaviour as before?
$$anonymous$$ake sure that your direction vector points in the same direction as the missile.
Also, if you have any physics enabled, it will not work this way, because the physics will conflict with your manual transformations.
If it still does not work, you can post your current code.
Hey, Senad, thanks for responding! No, it's not the same incorrect behaviour. Now it seems to make the same motion every time, regardless of how I modify the code. It sort of rotates around the point it's supposed to head towards... In any case, I've reposted the newer code over the older code in the original post...what do you think, now?
Also, no rigidbody on the missile, so no worries about that.