我的代碼按預期作業,直到我添加了定義當前健康 (currentHealth = health;) 和 (anim.SetTrigger("attacked")) 的行
然后我更改為您在下面看到的內容,以確保這些值與以前不同。
即使在更改后我也遇到了同樣的問題,我的敵人會在游戲開始時卡在命中影片的第 1 幀中。傷害和健康都很好,所以我完全不知道可能是什么問題。任何建議將不勝感激,如果需要,我可以在評論中添加有關我的代碼的更多資訊。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public float health;
public float currentHealth;
private Animator anim;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
currentHealth = 100;
health = 100;
}
// Update is called once per frame
void Update()
{
if (health < currentHealth)
currentHealth = health;
anim.SetTrigger("attacked");
if (health <= 0)
Debug.Log("Enemy is dead");
}
}
uj5u.com熱心網友回復:
所以你有幾個問題。{ }由于邏輯上有多行,您的第一個保護陳述句的主體應該用大括號括起來。縮進是不夠的。
更改行:
if (health < currentHealth)
currentHealth = health;
anim.SetTrigger("attacked");
...至:
if (health < currentHealth)
{ // <--- new
currentHealth = health;
anim.SetTrigger("attacked");
} // <--- new
此外,您在變數名稱中有錯字 - 您想將“當前健康”與“最大值”進行比較。
將其更改為如下所示:
if (currentHealth < health)
按照你的代碼,我不禁覺得你需要在播放影片之前在你的守衛陳述句中添加一些標志(除了檢查健康水平),而不是簡單地通過健康水平并將敵人的健康重置為最大健康每次他們都受到攻擊。否則,影片將從頭開始播放,永遠不會前進。
讓我們像這樣介紹標志_isPlayingAnimation:
private bool _isPlayingAnimation;
void Update()
{
if (currentHealth < health && !_isPlayingAnimation) // <--- updated
{
currentHealth = health;
anim.SetTrigger("attacked");
_isPlayingAnimation=true; // <--- new
}
.
.
.
}
.
.
.
// elsewhere, when the animation completes
_isPlayingAnimation = false; // <--- new
其他提示
考慮重命名變數。health有點模棱兩可。也許maxHealth會更好?這將避免諸如原始問題行之類的行混淆health < currentHealth。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/533282.html
標籤:C#unity3d
上一篇:多個碰撞同時發生
