【发布时间】:2020-05-07 05:46:57
【问题描述】:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.AI;
public class NavigateAgent : MonoBehaviour
{
public List<Transform> points = new List<Transform>();
public List<GameObject> npcs;
public NavMeshAgent agent;
private int destPoint = 0;
void Start()
{
var wayPoints = GameObject.FindGameObjectsWithTag("Waypoint");
foreach (GameObject waypoint in wayPoints)
{
points.Add(waypoint.transform);
}
npcs = GameObject.FindGameObjectsWithTag("Npc").ToList();
//agent = GetComponent<NavMeshAgent>();
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
agent.autoBraking = false;
GotoNextPoint();
}
void GotoNextPoint()
{
// Returns if no points have been set up
if (points.Count == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Count;
}
void VisitNpcs()
{
var npc = npcs[Random.Range(0, npcs.Count)];
var distance = Vector3.Distance(npc.transform.position, agent.transform.position);
if (distance < 3f)
{
// Stop slowly agent.
// Rotate agent and the npc at the same time slowly smooth to face each other.
}
}
void Update()
{
// Choose the next destination point when the agent gets
// close to the current one.
if (!agent.pathPending && agent.remainingDistance < 0.5f)
GotoNextPoint();
}
}
如果代理在路点之间移动时距离小于 3 距离随机挑选的 NPC 之一慢慢停止代理,但速度足够快,不会通过小于 3 的 NPC 距离,那么 NPC 和代理都应该面向彼此平滑旋转.
旋转部分结束后,他们面对面做一些事情。 在这个“做某事”部分结束后,让代理平滑地旋转回到原来的位置,然后再次移动他以继续移动航点。我想阻止他并旋转....但这更像是暂停他,代理旋转做一些事情,然后继续在航点之间移动。
每次代理访问 npc 调用它的代理暂停。逻辑是暂停代理并继续。
【问题讨论】: