- Home /
Enemy Performing only "Attack1" but not "Attack2" & "Attack3" I want Enemy perform All Attacks one by one.
public class EnemyAI : MonoBehaviour
{ public Transform Player; private Animator anim; private Rigidbody rb; public float MoveSpeed = 1f; public float MaxDist = 10f; public float MinDist = 1f;
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
}
void Update()
{
EnemyMovement();
EnemyAttack();
}
void EnemyMovement()
{
if (Vector3.Distance(transform.position, Player.position) <= MinDist)
{
Walk(false);
}
else
if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
{
transform.LookAt(Player);
transform.position = transform.position + transform.forward * MoveSpeed * Time.deltaTime;
Walk(true);
}
else
{
Walk(false);
}
}
void EnemyAttack()
{
if (Vector3.Distance(transform.position , Player.position) <= MinDist)
{
anim.SetTrigger("Attack1");
}
if (Vector3.Distance(transform.position, Player.position) <= MinDist)
{
anim.SetTrigger("Attack2");
}
if (Vector3.Distance(transform.position, Player.position) <= MinDist)
{
anim.SetTrigger("Attack3");
}
}
void Walk(bool move)
{
anim.SetBool("Movement", move);
}
Answer by slythethieve · Sep 12, 2019 at 12:15 PM
Why don't you remove the other 2 if statements and put everything in the first? So when the enemy is within distance you can perform all 3. Notify if there is something else going on after the first attack has been triggered.
Answer by imrankhanswati · Sep 12, 2019 at 12:26 PM
@gamo_logics Its because at "EnemyAttack()" funtion the first condition got true so Enemy plays "Attack1" but at the same time trigger for "Attack2" and "Attack3" been called but i guess there is no transition form "Attack1" to "Attack2". so If want to play all of these animation in sequence then you have to make transition from "Attack1" to "Attack2" and from "Attack2" to "Attack3". some thing like:
][1]
also put all of the animator calls withing same one "If" like this:
if (Vector3.Distance(transform.position , Player.position) <= MinDist)
{
anim.SetTrigger("Attack1");
anim.SetTrigger("Attack2");
anim.SetTrigger("Attack3");
}
@gamo_logics : Can you confirm that if "Enemy$$anonymous$$ovement()" got called then "Attack_1","Attack_2" and "Attack_3" trigger been register with animator. you can check this in parameter section of the animator.
Parameters are all set fine.. All the set to trigger
Answer by gamo_logics · Sep 12, 2019 at 12:37 PM
@imrankhanswati Here is the transition I try different ways but failed to achieve what I want..
Have you tried doing something like @imageimrankhanswati said? Don't put any parameters between the attacks and set the exit time to 1.
Yes I Tried it But I dont know whats wrong.. :)
It works fine when I Enter the Enemy $$anonymous$$imum Distance limit and then exit from it then it perform all the attacks but it do perform only first attack when I reach the $$anonymous$$inimum Distance limit..
Your answer
![](https://koobas.hobune.stream/wayback/20220612212049im_/https://answers.unity.com/themes/thub/images/avi.jpg)