【发布时间】:2020-05-08 23:26:52
【问题描述】:
当右侧的机器人和左侧的 npc 特工相遇时,它们也在面对面旋转,并且特工似乎正在看着 npc,但 npc 被冻结在这个位置,因为我禁用了npc 动画组件。
现在我想让npc也看代理。
npc在这种情况下我可以改变他的头部以及头部的顶端位置和旋转。 看起来当我禁用 npc 朝下的动画师时。我可以让 npc 改变动画到空闲,但仍然不知道如何让他看代理机器人。
我可以改变 X 上的头部旋转,这样看起来更像是他在看代理:
Changing the head rotation on X
如果我使用变换查看,npc 会倒在他的背上:
Using lookat the npc is falling on his back
最后我看到代理机器人也没有看代理。但是代理机器人没有头部我需要在 X 上旋转整个机器人
我想要做的是当 npc 和 agent 相互旋转时,它们也会看起来相互平滑。
机器人有组件:Animator,box ocllider,rigidbody,navmeshagent
npc有组件:动画师
这是我在 VisitNpcs 方法中用于轮换(会议)的脚本:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.AI;
public class Waypoints : MonoBehaviour
{
public List<Transform> points = new List<Transform>();
public List<GameObject> npcs;
public NavMeshAgent agent;
private int destPoint = 0;
private int damping = 2;
void Start()
{
var wayPoints = GameObject.FindGameObjectsWithTag("Waypoint");
foreach (GameObject waypoint in wayPoints)
{
points.Add(waypoint.transform);
}
npcs = GameObject.FindGameObjectsWithTag("Npc").ToList();
// 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 > 7)
{
agent.destination = npc.transform.position;
var collider = agent.GetComponent<BoxCollider>();
if (collider != null)
{
collider.enabled = false;
}
}
if (distance < 2.5f)
{
agent.isStopped = true;
npc.GetComponent<Animator>().enabled = false;
Vector3 lookPos = agent.transform.position - npc.transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
npc.transform.rotation = Quaternion.Slerp(npc.transform.rotation, rotation, Time.deltaTime * damping);
//npc.transform.LookAt(agent.transform);
}
}
void Update()
{
// Choose the next destination point when the agent gets
// close to the current one.
if (!agent.pathPending && agent.remainingDistance < 0.5f)
{
GotoNextPoint();
}
VisitNpcs();
}
}
【问题讨论】: