Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to think in proper physics terms. You have a velocity, and you want to add an acceleration. That's all there is to it - the acceleration is a gradual change in velocity that will draw the enemy towards to player, allow it to overshoot, slow down (or turn) and then head back towards the player.</p> <p>Acceleration is measured as d(velocity)/time. You want to accelerate towards the player at any point in time, so every interval (second, hundredth of a second or whatever you choose) you need to add the vector between enemy and player, multiplied by some constant, to your velocity.</p> <pre><code>Velocity = Velocity + c * (Player-Enemy vector) </code></pre> <p>Constant c will depend on how fast you want to accelerate towards the player, and how often you are updating your velocity.</p> <p>If you want to 'cap' the maximum speed of the enemy so that it doesn't continue to increase the magnitude of its velocity indefinitely, you can also do so.</p> <pre><code>Velocity = Velocity * (Maximum magniture / |Velocity|) </code></pre> <p>EDIT: to clarify further, adding a Velocity simply means adding the component vectors. So </p> <pre><code>Vx = Vx + c * Ax Vy = Vy + c * Ay </code></pre> <p>where V is velocity and A is acceleration. Magnitude is measured as <code>sqrt(Vx^2 + Vy^2)</code>, ie the hypotenuse of a right triangle. So if you want maximum speed of the enemy to be m,</p> <pre><code>Vx = Vx * ( m / sqrt(Vx^2 + Vy^2) Vy = Vy * ( m / sqrt(Vx^2 + Vy^2) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload