【问题标题】:How can I make the npc look at the agent with the head or using the transform.lookat?如何让 npc 用头部或使用 transform.lookat 查看代理?
【发布时间】:2020-05-08 23:26:52
【问题描述】:

当右侧的机器人和左侧的 npc 特工相遇时,它们也在面对面旋转,并且特工似乎正在看着 npc,但 npc 被冻结在这个位置,因为我禁用了npc 动画组件。

现在我想让npc也看代理。

npc在这种情况下我可以改变他的头部以及头部的顶端位置和旋转。 看起来当我禁用 npc 朝下的动画师时。我可以让 npc 改变动画到空闲,但仍然不知道如何让他看代理机器人。

Npc facing down

我可以改变 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();
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    为了看起来平滑,你可以使用协程并设置允许旋转的时间:这里我们允许 2s 旋转到 PositionToLook.. 指定的时间自动调整旋转速度

        StartCoroutine(RotateToDirection(player, target.position, 2f));
    

    public IEnumerator RotateToDirection(Transform transform, Vector3 positionToLook, float timeToRotate)
    {
        var startRotation = transform.rotation;
        var direction = positionToLook - transform.position;
        var finalRotation = Quaternion.LookRotation(direction);
        var t = 0f;
        while (t <= 1f)
        {
            t += Time.deltaTime / timeToRotate;
            transform.rotation = Quaternion.Slerp(startRotation, finalRotation, t);
            yield return null;
        }
        transform.rotation = finalRotation;
    }
    

    所以你启动了 2 个相同的协程,一个用于 NPC,另一个用于 Droid。

    我不知道你的 NPC 是怎么做的,但我建议你创建一个空的 GameObject 并将头部的所有元素像孩子一样放置,然后旋转 Empty Gameobject.. 我想身体的其余部分有动画......(腿,脚……)

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 2018-12-16
      • 2010-11-28
      • 2016-01-14
      • 2017-01-05
      • 1970-01-01
      • 2013-03-14
      • 1970-01-01
      • 2019-02-17
      相关资源
      最近更新 更多