Hello,
I have two major problems with this AI script since I have tryed to modify a simple animation.
- As the chasing becomes true the running animation doesn’t play ( the idle is working normal )
- 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;
}
}