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之一开始慢慢停止智能体,但速度足够快,不能超过npc的距离小于3,那么npc和智能体都应该面向对方平滑旋转。
在旋转部分结束后,他们面对面做一些事情。在这个“做点什么”部分结束后,让智能体平滑地向后旋转到它原来的位置,然后再次移动他继续移动航点。我想阻止他和rotate....but,这更像是让他停下来,让特工轮流做些事情,然后继续在路点之间移动。
每次代理访问npc时,将其称为代理的暂停。逻辑是暂停代理并继续。
发布于 2020-05-07 22:04:31
-Stop Navmesh代理脚本
Transform destinationPoint=(create and store temporary destination point)
gameObject.GetComponent<NavMeshAgent>().Stop();
-then在慢动作中手动旋转对象并执行其他操作
IEnumerator RotateAnimation(float from, float to)
{
float time = 0.02f;
int speedOfRotation = 1;
while (from < to)
{
yield return new WaitForSeconds(time);
from += speedOfRotation;
gameObject.transform.Rotate(0, speedOfRotation, 0);
}
}
然后-and设置回目标点
gameObject.GetComponent<NavMeshAgent>().SetDestination(destinationPoint);
我认为这将对您有所帮助;)
https://stackoverflow.com/questions/61650740
复制相似问题