Assuming the creep can only move in 4 directions (up, down, left right), you could make a series of if statements checking for the direction.
if (directionVector.x == 1 && directionVector.y == 0){
//direction is right
} else if (directionVector.x == -1 && directionVector.y == 0){
//direction is left
} else if (directionVector.x == 0 && directionVector.y == 1){
//direction is up
} else if (directionVector.x == 0 && directionVector.y == -1){
//direction is down
}
Then, in each of the if statements, you can use a 2d toolkit object called tk2dSpriteAnimator. Save the 4 animations as tk2dSpirteAnimationClips, and then you can use
tk2dSpriteAnimator.Play(tk2dSpriteAnimationClip clip);
to play the specific animation. So your final code segment should be something like:
var animator : tk2dSpriteAnimator;
var rightAnim : tk2dSpriteAnimationClip;
var leftAnim : tk2dSpriteAnimationClip;
var upAnim : tk2dSpriteAnimationClip;
var downAnim : tk2dSpriteAnimationClip;
if (directionVector.x == 1 && directionVector.y == 0){
animator.Play(rightAnim);
} else if (directionVector.x == -1 && directionVector.y == 0){
animator.Play(leftAnim);
} else if (directionVector.x == 0 && directionVector.y == 1){
animator.Play(upAnim);
} else if (directionVector.x == 0 && directionVector.y == -1){
animator.Play(downAnim);
}
EDIT: Just noticed that you also wanted them to move at an angle. In that case you check which direction that angle is closest to (like any angle between 45 degrees and 135 degrees would play an up animation). Again, this can be done with a variety of if statements.
var movementAngle = Mathf.Atan(directionVector.y/directionVector.x);
if (movementAngle >= Mathf.PI/4 && movementAngle < 3*(Mathf.PI/4)){
//go up
} else if (movementAngle >= 3*(Mathf.PI/4) && movementAngle < 5*(Mathf.PI/4)){
//go left
} else if (movementAngle >= 5*(Mathf.PI/4) && movementAngle < 7*(Mathf.PI/4)){
//go down
} else {
//go right
}
↧