我試圖讓敵人攻擊玩家,敵人通過影片進行攻擊,但是當其他人處于活動狀態時,我必須防止影片開始的方法不起作用。這個問題應該由顯示這(可能是低質量)。如您所見,問題在于它不斷打開和關閉布林值。我不知道為什么會發生這種情況,因為我的方式應該有效。有沒有辦法阻止他們這樣做并一次運行一次?這是我的代碼:
編輯:我忘了說這個,但我的代碼沒有出現任何錯誤
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
uj5u.com熱心網友回復:
我認為您的主要問題可能是您AttackPlayer()重復呼叫該函式。如果是這種情況(編輯:您更新了代碼,看起來就是這種情況),那么它會MultAttack()重復呼叫,這將導致它每次(可能每幀)選擇一個新的隨機攻擊影片播放。
我還看到了一些可能有影響但很難說的小問題:
從您的代碼:
if (distDif >= AttackRange) { agent.SetDestination(playerPos.transform.position); anim.SetBool("walk", false); }
您真的要在這里將“行走”影片設定為 false 嗎?
其他:
您的代碼:
MultAttack(playerPos, anim, agent, self, MultiAttack, inRange, inView); if (!MultiAttack) { anim.SetBool("Attack2", false); anim.SetBool("Attack3", false); StartCoroutine(PlayAnim(anim, "Attack")); }
MultAttack()即使變數MultiAttack為假,您也在呼叫。而不是僅僅“撤消”你所做的一切MultAttack()ifMultiAttack是假的,也許只是不要呼叫它,除非MultiAttack是真的?像這樣:
if(MultiAttack)
{
MultAttack(/*arguments here*/);
}
else
{
//trigger single attack animation
}
我還強烈建議通過這些函式將所有資料作為單個物件或結構或其他東西傳遞。這看起來像是應該存盤在“敵人”物件上的那種資料。擁有所有這些引數使得很難判斷發生了什么并且很難組織。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/381519.html
