Enemy AI

Hello,

I have two major problems with this AI script since I have tryed to modify a simple animation.

  1. As the chasing becomes true the running animation doesn’t play ( the idle is working normal )
  2. With the audio.clip is happening the same.

How could this be ?

var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var attackThreshold = 3; // distance within which to attack
var chaseThreshold = 10; // distance within which to start chasing
var giveUpThreshold = 20; // distance beyond which AI gives up
var attackRepeatTime = 1; // delay between attacks when within range
var attack : AudioClip;
var controller:CharacterController;

private var target : Transform; //the enemy's target
private var initialSpeed = 0;
var chasing = false;
private var attackTime = 1;


var myTransform : Transform; //current transform data of this enemy

function Awake()
{
    myTransform = transform; //cache transform data for easy access/preformance
}

function Start()
{
	
	controller = gameObject.GetComponent(CharacterController);
	
     target = GameObject.FindWithTag("Player").transform; //target the player
      if (!target)
      Debug.Log ("ERROR : player not tagged as 'Player'");
    
    // Set all animations to loop
	animation.wrapMode = WrapMode.Loop;
		
	// Put idle and run in a lower layer. They will only animate if our action animations are not playing
	animation["idle_1"].layer = -1;
	animation["running_zombie"].layer = 1;
	
	animation.Stop();
}

function Update () {

	Debug.DrawLine( target.position , myTransform.position , Color.cyan);
	
    // check distance to target every frame:
    var distance = (target.position - myTransform.position).magnitude;
	
	// Starts chasing
        if (distance < chaseThreshold) {
            chasing = true;
        }
	
	
    if (chasing) {

		transform.LookAt(target);  // Look at the player
        controller.SimpleMove(moveSpeed*transform.forward);
        audio.clip = attack;
    	audio.Play();
		animation.CrossFade("running_zombie");
		animation.Play();
       }
       	else { 
       	       	animation.Stop("running_zombie"); 
       	       	animation.CrossFade("idle_1");
				animation.Play("idle_1");
      		 	}	

        
        // give up, if too far away from target:
        if (distance > giveUpThreshold) {
            chasing = false;
        }
 
 										
}

I want to say that there is a problem stopping your animation when he goes idle. Try and take out the animation.Play(), I think CrossFade calls Play already so you could be playing it twice.

In this situation you dont need layers, so remove them. It will also mean you dont need to worry about stopping animations

Dex is right about crossfade/play doing pretty much the same thing.

I think regarding the animation…

You tell it to cross fade, which is good, but then you immediately tell it to Play() the current animation, which is still idle_1 at this point. So if you put ‘Play(“running_zombie”)’ like you did in your else block, it should work, but you don’t get the cross fade effect.

The cross fade makes it nicer, so there isn’t an immediate difference in the position of the animated limbs, so I would agree with both JamesLeeNZ and DexRobinson, that just going with animation.Crossfade([animation id]) is the best route, and just leave the animation.Play() off altogether.

As far as your sound goes, I think it is playing, but you must have no noise at the start of the audio clip, because it looks like you are re-starting it every single update loop when the chasing happens. If you had noise at the start of the audio clip, you would hear a constant, super annoying studdering sound. The other issue, is maybe you forgot to drag and drop an audio clip to your ‘attack’ variable within the editor?

You only want to play the noise at the start of the chase, not every update loop while chasing right? So maybe doing it like this would be better:

if(threshold > distance){
    if(false == chasing){
      chasing = true;
      audio.clip = attack;
      audio.Play();
    }
}

JamesLeeNZ is also correct about not needing layers for this situation. Layers are used for blending animations together, it isn’t a ‘priority’ type thing. Here is a good explanation:

As stated in the docs, animations in higher layers will have their weights distributed first, before lower layers. For example, let’s say you have a walk cycle animation that affects the whole body, and a waving animation that only affects the upper body. If they were on the same layer and then blended, then the arms would partially walk / partially wave. If the wave animation was on a higher layer and then the two blended, then the arm would wave normally while the lower body could keep walking.

Thanks a lot !
I wil be trying right now.

*Edit

The sound is working now but it seems that the animation is not working on that character (Through I have got it for that character )