[英]OnEnable not updating variables - Unity3D
我有一群敵人,跟隨玩家在游戲中被激活/停用。 問題是一旦它們被重新激活,它們會保留在被停用之前的剛體.速度值,並且不會像我在 OnEnable() 函數上寫的那樣更新它們,所以它們只是去其他地方而不是跟隨玩家。 我的代碼如下:
public class EnemyFollow : MonoBehaviour
{
public GameObject playerPos;
protected Rigidbody rb;
bool secondSpawn = false;
private void OnEnable()
{
if (secondSpawn)
{
Vector3 dir = new Vector3(playerPos.transform.position.x - transform.position.x,
playerPos.transform.position.y - transform.position.y,
playerPos.transform.position.z - transform.position.z).normalized;
rb.velocity = dir;
}
}
void Start()
{
playerPos = GameObject.FindGameObjectWithTag("Player");
rb = GetComponent<Rigidbody>();
Vector3 dir = (playerPos.transform.position - transform.position).normalized;
rb.velocity = dir;
secondSpawn = true;
}
我確定我犯了一個菜鳥錯誤,但我就是想不通。 感謝您的關注。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.