所以我目前正在嘗試創建一個僵尸游戲,在那里我撰寫了一個腳本,如果它“靠近”玩家,它將啟用一個擊球影片。該腳本的代碼如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ZombieNavMesh : MonoBehaviour
{
private Transform movePositionTransform;
private NavMeshAgent navMeshAgent;
private Animator animator;
static bool close;
private void Start()
{
movePositionTransform = GameObject.Find("Player").transform;
animator = GetComponent<Animator>();
}
private void Awake()
{
navMeshAgent = GetComponent<NavMeshAgent>();
}
private void Update()
{
if (!close)
{
navMeshAgent.destination = movePositionTransform.position;
animator.SetBool("Close", false);
}
if (close)
{
animator.SetBool("Close", true);
}
}
public void OnTriggerEnter(Collider other)
{
if(other.tag == "Player")
{
Debug.Log("Should hit");
close = true;
}
}
public void OnTriggerExit(Collider other)
{
if(other.tag == "Player")
{
Debug.Log("Should not hit");
close = false;
}
}
}
這里的問題是,如果一個僵尸進入觸發器,另一個僵尸退出觸發器,布林值 close 將被設定為 false,因此兩者都會執行行走影片;它應該是“近距離”僵尸撞擊影片,以及遠距離僵尸行走影片。
uj5u.com熱心網友回復:
將單詞static從static bool close;
static意味著該類的所有版本和實體僅共享一個東西。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/463669.html
上一篇:影片兩個圓圈Python
下一篇:我需要在C 專案中選擇隨機函式
