我正在為我的游戲開發一個敵人,它會跟隨你直到它不再看到你。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class pathfindingPlayer : MonoBehaviour
{
RaycastHit hit;
NavMeshAgent _navMeshAgent;
void Awake() => _navMeshAgent = GetComponent<NavMeshAgent>();
public bool seen;
public float noticeDistance = 7f;
void Update()
{
Debug.DrawRay(transform.position, transform.forward * noticeDistance, Color.red);
Ray PlayerRay = new Ray(transform.position, Vector3.back);
if(Physics.Raycast(PlayerRay, out RaycastHit hitInfo, noticeDistance))
{
if (hitInfo.collider.CompareTag("Player"))
{
Vector3 SeenPlayer = hitInfo.point;
_navMeshAgent.SetDestination(SeenPlayer);
}
}
}
}
現在的問題是只有一個 Raycast 被射出。因此,敵人只向一個方向跑。有沒有辦法為此添加多個光線投射,還是我需要重寫代碼?提前致謝
uj5u.com熱心網友回復:
您可以創建一個進行光線投射的函式并每隔 x 秒呼叫一次,但是對于您想要實作的目標,我會嘗試在敵人身上放置一個圓形對撞機,其半徑是敵人可以看到的距離,并確保將對撞機設定為觸發,然后如果玩家觸發對撞機,則使其朝向玩家
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/435361.html
