Stop an animation. Start a new one.

I’m sorry gang. I KNOW there are topics about this. I know that this has already been covered. I’ve read the posts, I’ve googled, and am no closer to figuring out how to make my npc do what I want him to. Essentially, I have an AI script that starts the “walk” animation when the player is a certain distance away. That works just fine. When the npc gets close, though, he’s supposed to stop and his animation is supposed to switch from “walk” to “attack.” Well, once in the “stop distance” area, the npc does indeed stop moving toward the player, but his original walk animation keeps playing, and the attack animation never fires. Have tried animation.Stop(); as well as crossfade, with no change whatsoever. Am hoping that I simply have my animation commands in the wrong place, and that fresh eyes will be able to see what I’m doing wrong. Code is below. Thanks, and God bless.

(please note: I actually have TWO AI scripts. One that handles the movement toward the player (no animations attached) and one that handles the attack. Why did I do it that way? Heck if I remember, will probably consolidate them soon.)

Script with Animation Attached

var target : Transform;
var moveSpeed = 5;
var rotationSpeed = 5;
var myTransform : Transform;
var minDistance = 5;
var follow : boolean = true;
var damage = 8;





     
function Awake()
{
myTransform = transform;
}
     
function Start()
{
     
target = GameObject.FindWithTag("Player").transform;
     
}

     
     function Update () {
     
     
     if(Vector3.Distance(myTransform.position, target.position) < minDistance)
     
     animation.Play("BDGuardWalk"); 
     }

   
    
      
      


       
       
       function OnTriggerEnter (col : Collider) {
 
       if(col.gameObject.tag == "Player") { 
       animation.CrossFade("BDGuardWalk");
       animation.Play("BDGuardAttack");
       Playerhealth.curHealth -= damage;

 
 
       }
           
    
       

       
       
       
      

       



       
}

1 Answer

1

A) You should not have multitudes of blank lines in your code

B) When you’re checking (in the Update method) for distance, you are saying:

If the distance between me and the player is less than minimum distance, play the walk animation.

I think you meant “it it’s greater than” - not less than.

Hey Dracorat, A) Thanks. Was trying to make it easier to read, but sorry if I followed bad form B) That's correct. The minimum distance is the distance at which the npc notices the player. Although, I can definitely see where that would cause an issue, since the StopDistance (of script 2) is within that distance. So perhaps the solution is an AND statement of some kind? (for instance: if player distance is less than minDistance and less than Stop distance then fire second animation?)

Yes, then you should have a second if inside the first. If distance < melee range, play melee, otherwise play walk animation. If you want to do crossfading, you'll have to do stateful checking which means "what was I doing before?" and then only fire the animation if "before" doesn't match "now". If distance < melee range - what was I doing? - - I was meleeing - carry on then - - I was walking - cross fade to melee and record my last state as melee - etc...

Dracorat, your solution worked. Thanks so much! Please convert it to an answer, if you wouldn't mind :)

It is - just press the check mark.

sorry, meant the last comment above. "If distance < melee range" etc.