I'm working on a zombie game in AS3 where I want a character to be able to move away from a zombie depending upon how close the zombie is. The character also has a destination that it's trying to get too on the screen.

Ok so I have 2 vectors, one pointing to my destination, and one pointing to the zombie which I then invert to get my "away" vector. I then turn the distance between my character and the zombie into a value between 0 and 1. And then I'm stuck on how to get a resultant vector for my character. How would I use my 0-1 value to calculate how much of the away vector is used and how much of the original destination vector is still left if that makes sense? to end up with 1 direction vector to move my character? So if the zombie is right where my character is, then my direction vector = away vector, and if I'm far away from the zombie than my direction vector = destination vector, but how do I calculate the in-between?

Ideally need the answer in AS3.


Simply add the vectors together, after scaling them.


 CharacterDirection = DestinationVector + AwayVector

Where awayFactor is your 0-1 value.

Sorry, I don't know AS3, so I can't write it in that language.

If it's just x and y vectors this is the same as:

DestVec.x *= (1-awayFactor)
DestVec.y *= (1-awayFactor)

AwayVec.x *= awayFactor
AwayVec.y *= awayFactor

CharVec.x = DestVec.x + AwayVec.x
CharVec.y = DestVec.y + AwayVec.y
  • \$\begingroup\$ You can find the AS3 specific stuff here \$\endgroup\$ – MichaelHouse Jun 23 '12 at 22:18

I would try ordinary linear interpolation. If your destination vector is D (dx, dy, dz) and away vector is A (ax, ay, az), and your 0-1 value is V, then final vector F would be:

F = V * A + (1 - V) * D = (V * ax + (1 - V) * dx, ....)

If V is 1 (as in your example), you will get F = 1 * A + (1 - 1) * D = A, just as in your example.

But you should be careful with 0-1 value (if it isn't in range 0-1, then don't use interpolation) and also with vector normalization (I don't know, if you are using normalized vectors or not).

  • \$\begingroup\$ Thanks for the reply, ideally I need that in AS3 :) my vectors are just simple, x, y vectors, no z value. \$\endgroup\$ – Phil Jun 23 '12 at 22:07
  • \$\begingroup\$ @Phil It's just a simple formula using vectors. Write it yourself in AS3. \$\endgroup\$ – doppelgreener Jun 23 '12 at 22:59
  • \$\begingroup\$ @Jonathan I understand AS3, not mathematical notation, but hey thanks for your really positive comment anyway. \$\endgroup\$ – Phil Jun 23 '12 at 23:54
  • \$\begingroup\$ @Phil It's only basic algebra, otherwise I wouldn't have said anything. \$\endgroup\$ – doppelgreener Jun 24 '12 at 0:10

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.