x


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);
    }
}
more ▼

asked Apr 20 '12 at 05:29 AM

Catlard gravatar image

Catlard
527 47 57 62

(comments are locked)
10|3000 characters needed characters left

1 answer: sort oldest

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.

more ▼

answered Apr 20 '12 at 06:42 AM

senad gravatar image

senad
641 3 5

Ah, good plan, let me try that...

Apr 20 '12 at 06:53 AM Catlard

Hmm...I know what you mean, but how do I determine what the vector will be adjusted by?

Apr 20 '12 at 07:11 AM Catlard

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?

Apr 20 '12 at 07:35 AM Catlard

Is it the same wrong behaviour as before?

Make 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.

Apr 20 '12 at 08:13 AM senad

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.

Apr 20 '12 at 12:17 PM Catlard
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x289
x286
x102
x41
x4

asked: Apr 20 '12 at 05:29 AM

Seen: 746 times

Last Updated: Apr 21 '12 at 11:20 AM