I have my enemy following my player and the animations working for the walking and idle states. What I need now is to have the player stop and use one attack animation then return to the idle animation, each time he attacks. Right now every time the enemy attacks he kicks his foot and glitches(no matter what the animation is)
I assume that this has something to do with the wrapmode and different animations being called at the same time or something. I am fairly new to unity so im not sure.
Here is the enemy AI script, and the attack script:
EnemyAttack.cs:
privatevoidAttack(){float distance =Vector3.Distance(target.transform.position, transform.position);
Vector3 dir =(target.transform.position - transform.position).normalized;float direction =Vector3.Dot(dir, transform.forward);
PlayerHealth ph =(PlayerHealth) target.GetComponent(“PlayerHealth”);
if(distance <2.5f&& direction >0){
animation.Play(“attack1”);
ph.AdjustCurrentHealth(-10);}
}
EnemyAI.cs
voidAwake(){
myTransform = transform;
moveSpeed =3;
rotationSpeed =5;
maxDistance =2.3f;
animation.wrapMode =WrapMode.Loop;}
// Use this for initializationvoidStart(){
maxDistance =2;}
// Update is called once per framevoidUpdate(){if(target ==null){
target =GameObject.FindGameObjectWithTag(“Player”).transform;}
Debug.DrawLine(target.position, myTransform.position,Color.cyan);
//Look at target
myTransform.rotation =Quaternion.Slerp(myTransform.rotation,Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed *Time.deltaTime);
if(Vector3.Distance(target.position, myTransform.position)> maxDistance){//Move towards target
myTransform.position += myTransform.forward * moveSpeed *Time.deltaTime;
animation.CrossFade(“walk1”);}else{
animation.CrossFade(“idle”);}
}
If someone could provide an answer I would be very greatful. Thanks!