我明白我需要做什么才能讓我的腳本作業我只是不知道要寫什么代碼。我想要做的是將我的 currentHealth int(不是公共 int,只是一個 int)從我的 Enemy 腳本到我的 Enemy Combat 腳本,這樣我就可以在我的 currentHealth 高于 50 時使我的第二次攻擊做 0 dmg。如果我真的想要回到真正的問題,這是我的第二次攻擊,雖然當 hp 為 50 或更高時影片不會播放,因為該攻擊只應該在 50 hp 或更低時發生,但碰撞檢查仍然起作用并損壞玩家,即使它是直到 50 hp 或更低時才設定為激活。更詳細地說,我將我的 Boss 分成多個階段,第一階段是直到你將 Boss dmg 到 50hp 然后他進入第二階段并進行第二次攻擊。該功能運行良好,但是,
這是保存健康邏輯的 Enemy 腳本。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public Component enemyCombat;
public Component aiScript;
public Animator animator;
public int maxHealth = 100;
int currentHealth;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
animator.SetTrigger("Hurt");
if (currentHealth <= 50)
{
animator.SetBool("IsAngry", true);
}
if(currentHealth <= 0)
{
Die();
}
}
void Die()
{
Debug.Log("Enemy Died");
animator.SetBool("IsDead", true);
Destroy(aiScript);
Destroy(enemyCombat);
GetComponent<Collider2D>().enabled = false;
this.enabled = false;
}
這是我的 EnemyCombat 腳本(以我的老板命名,這是 Orion 的升級版本)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OrionUpgradedCombat : MonoBehaviour
{
public Animator animator;
public GameObject gameObject;
public Transform AttackPoint;
public Transform secondAttackPoint;
public float attackRange = 0.5f;
public float secondAttackRange = 0.5f;
public LayerMask enemyLayers;
public int attackDamage = 8;
public int secondAttackDamage = 15;
public float attackRate = 2f;
float nextAttackTime = 0f;
void Start()
{
enemy enemyScript = gameObject.GetComponent<Enemy>();
}
// Update is called once per frame
void Update()
{
if (Time.time >= nextAttackTime)
{
StartCoroutine(Attack());
StartCoroutine(AttackTwo());
nextAttackTime = Time.time 8f / attackRate;
}
if (currentHealth >= 50)
{
secondAttackDamage -= 15;
}
else
{
secondAttackDamage = 15;
}
}
public IEnumerator Attack()
{
yield return new WaitForSeconds(2);
animator.SetTrigger("Attack");
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(AttackPoint.position, attackRange, enemyLayers);
foreach (Collider2D enemy in hitEnemies)
{
enemy.GetComponent<PlayerHealth>().TakeDamage(attackDamage);
}
}
public IEnumerator AttackTwo()
{
yield return new WaitForSeconds(4);
animator.SetTrigger("AttackTwo");
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(secondAttackPoint.position, secondAttackRange, enemyLayers);
foreach (Collider2D enemy in hitEnemies)
{
enemy.GetComponent<PlayerHealth>().TakeDamage(secondAttackDamage);
}
}
void OnDrawGizmosSelected()
{
if (AttackPoint == null)
return;
Gizmos.DrawWireSphere(AttackPoint.position, attackRange);
}
uj5u.com熱心網友回復:
為什么它仍然將損壞的原因是因為你開始AttackTwo coroutine對你開始后AttackOne coroutine。在將您的值更改secondAttackDamage為 0之前,您也是這樣做的。我建議AttackTwo coroutine您在敵人的生命值低于 50 時呼叫它。
uj5u.com熱心網友回復:
如果你能在這種情況下使用影片事件就太好了,但如果由于任何原因無法實作,我想可能是在 Update() 方法中呼叫協程有問題,所以我建議使用一個標志來啟動協程當上一個通話結束時。它會是這樣的:
bool isCoroutineStarted = false;
void Update()
{
if (!isCoroutineStarted)
{
StartCoroutine("YourAttackCoroutine");
}
}
IEnumerator YourAttackCoroutine()
{
isCoroutineStarted = true;
//Your Code
yield return new WaitForSeconds(0);
isCoroutineStarted = false;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/365052.html
上一篇:通過旋轉另一個物件(hand-VR)來旋轉物件(模型)
下一篇:Unity專案未加載
