【发布时间】:2022-01-14 13:27:17
【问题描述】:
我试图让敌人攻击玩家并通过动画攻击敌人,但是当其他人处于活动状态时我必须阻止动画开始的方法不起作用。问题应该由this 显示(可能是低质量)。如您所见,问题在于它不断地打开和关闭布尔值。我不知道为什么会这样,因为我拥有它的方式应该可以工作。有没有办法阻止他们这样做并一次运行一个有效的?这是我的代码:
编辑:我忘了说这个,但我的代码没有任何错误
public void AttackPlayer(GameObject playerPos, Animator anim, NavMeshAgent agent, GameObject self, bool MultiAttack, float inRange, float inView, float AttackRange, bool isBlocking, bool HasSeenPLayer)
{
distDif = Vector3.Distance(self.transform.position, playerPos.transform.position);
if (distDif >= AttackRange)
{
agent.SetDestination(playerPos.transform.position);
anim.SetBool("walk", false);
}
else if (distDif <= AttackRange)
{
anim.SetBool("walk", false);
}
MultAttack(playerPos, anim, agent, self, MultiAttack, inRange, inView);
if (!MultiAttack)
{
anim.SetBool("Attack2", false);
anim.SetBool("Attack3", false);
StartCoroutine(PlayAnim(anim, "Attack"));
}
if (!PlayerPos.FindPlayerPos(AttackRange, playerPos.transform, inView, self, HasSeenPLayer))
{
anim.SetBool("Attack", false);
anim.SetBool("Attack2", false);
anim.SetBool("Attack3", false);
state = State.Chase;
}
}
private bool noloop = true;
public void MultAttack(GameObject playerPos, Animator anim, NavMeshAgent agent, GameObject self, bool MultiAttack, float inRange, float inView)
{
if (!MultiAttack) return;
if (random == 0) random = Random.Range(1, 5);
if (random == 1)
{
StartCoroutine(PlayAnim(anim, "Attack"));
random = 0;
return;
}
if (random == 2)
{
if (HasParameter("Attack2", anim))
{
StartCoroutine(PlayAnim(anim, "Attack2"));
random = 0;
return;
}
StartCoroutine(PlayAnim(anim, "Attack"));
random = 0;
return;
}
if (random == 3)
{
if (HasParameter("Attack3", anim))
{
StartCoroutine(PlayAnim(anim, "Attack3"));
random = 0;
return;
}
StartCoroutine(PlayAnim(anim, "Attack"));
random = 0;
return;
}
if (random == 4)
{
BlockPlayer(playerPos, anim, agent, inRange, inView, self);
random = 0;
return;
}
StartCoroutine(PlayAnim(anim, "Attack"));
random = 0;
return;
}
public static bool HasParameter(string paramName, Animator animator)
{
foreach (AnimatorControllerParameter param in animator.parameters)
{
if (param.name == paramName)
return true;
}
return false;
}
IEnumerator PlayAnim(Animator anim, string booleanName)
{
anim.SetBool(booleanName, true);
yield return new WaitForSeconds(anim.GetCurrentAnimatorStateInfo(0).length + anim.GetCurrentAnimatorStateInfo(0).normalizedTime);
//^^^ this has a value I had debug.Log print the value and it has one value
anim.SetBool(booleanName, false);
}
编辑 2:这是调用攻击函数的方式:(在其自己的脚本中)
public void ChoseChasingWhatStateToAttackPlayer(NavMeshAgent agent, GameObject playerPos, GameObject Gself, Animator anim, bool MultiAttack, float inRange, float inView, float AttackRange, bool isBlocking, bool HasSeenPLayer)
{
switch (state)
{
case State.Chase:
ChasePlayer(playerPos, Gself, anim, agent, AttackRange, inRange, inView, HasSeenPLayer);
break;
case State.Attack:
AttackPlayer(playerPos, anim, agent, Gself, MultiAttack, inRange, inView, AttackRange, isBlocking, HasSeenPLayer);
break;
case State.Idle:
IdlePlayer(playerPos, anim, agent, Gself, inRange, inView, HasSeenPLayer);
break;
case State.Wander:
WanderPlayer(playerPos, anim, agent, Gself, HasSeenPLayer);
break;
}
}
这是在主脚本中:
TtF.ChoseChasingWhatStateToAttackPlayer(agent, Player_pos.player, self, anim, MultiAttack, inRange, inView, AttackRange, isBlocking, hasSeenPlayer);
// called in update
【问题讨论】: