I'm building a game with players and missiles.

I want to fire a missile from P1 to P2. The missile should always take exactly five seconds to reach P2. The missile should also track P2. If P2 moves farther away, the missile must speed up to satisfy the timing constraint. If P2 moves left, the missile must move left (and change speed as well). If P2 moves closer to P1, the missile must slow down.

Is this possible? What's the best way to accomplish this?

I don't currently have a Vector2D class (this isn't actually a game, but the analogy works well), so if possible, I'd like a solution that doesn't require one.

Edit: This isn't actually a missile either. :)


  • 1
    \$\begingroup\$ (I'm somewhat scared of someone asking about missile guidance qualified by "this isn't actually a game" but…) Do you have any constraints on the shape of the missile's trajectory? Can it always follow a straight line between its current position and player 2? Does it have any limit on its turning radius or acceleration? Does it need to avoid obstacles? Also, you mention Vector2D - is it safe to assume you only need this in 2D? You can do all the vector math componentwise, but it's clunky to write compared to a simple vector math class, so I don't really understand the preference against this. \$\endgroup\$ – DMGregory Apr 16 '16 at 0:13
  • \$\begingroup\$ You missed some additional info. Are there constrains? Do missiles move along a line or should we care about directions, and how many dimensions eventually? \$\endgroup\$ – liggiorgio Apr 16 '16 at 0:31
  • \$\begingroup\$ The path should not be constrained. The only rules are: A) The "missile" should always take steps towards its target. B) The "missile" should always take 5 seconds to get there. Writing a Vector2D class is no problem. I just don't have one handy (and math isn't my best subject). If it makes it easier, I can add it. \$\endgroup\$ – Peter Apr 16 '16 at 0:56
  • \$\begingroup\$ The simplest way to do this is to use a hermite spline and lerp the parametric value. \$\endgroup\$ – Steven Srun Apr 16 '16 at 16:33

The edit is reassuring. :)

Okay, here's a straightforward update loop...

Assuming when we fire the missile we initialize remainingFlightTime = 5f then...

void UpdateMissile(float deltaTime)
   remainingFlightTime -= deltaTime;

   // At the end of the trajectory, snap to target & explode.
   // The math will put us there anyway, but this saves
   // on fancy logic related to overshooting. ;)
   if(remainingFlightTime <= 0f)
      myX = targetX;
      myY = targetY;

   // Compute straight-line velocity that will take us
   // to the target in exactly the time remaining.
   velocityX = (targetX - myX)/remainingFlightTime;
   velocityY = (targetY - myY)/remainingFlightTime;

   // Integrate the velocity into the position.
   myX += velocityX * deltaTime;
   myY += velocityY * deltaTime;
| improve this answer | |
  • 1
    \$\begingroup\$ This method does not make the "missile" appear to rotate around it's launch point, mid-flight. +1 \$\endgroup\$ – Jon Apr 16 '16 at 3:35

Why not lerp the position and update the target destination as it changes?

You might have to look at how lerping works but it would look something like this:

public position startMarker;
public float duration = 5.0f;
private float startTime;
void Launch() {
    startTime = Time.time;
void Update() {
    float distCovered = (Time.time - startTime) / duration;
    missile.position = Vector3.Lerp(startMarker.position, target.position, distCovered);

by lerping you are basically changing the position of an object over a certain amount of time so you could say that you are 50% of the way from A to B and the object would be placed there. Lerping is direct so you will always move towards the target (as you want in the comments).

| improve this answer | |
  • \$\begingroup\$ huh well my bad... i guess thats what happens if you try to code at 3am. changed the answer to fix the bug \$\endgroup\$ – user3797758 Apr 16 '16 at 11:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.