c# - Unity 2018 - NPC 在范围内平滑地转向面对玩家

标签 c# unity3d

使用当前代码,NPC 将检测到并转向播放所需动画的玩家。但是,NPC 只会快速面向玩家,而不会在玩家在 NPC 范围内绕过时继续旋转。

我想对此进行修改,以便当玩家处于对撞机范围内时,NPC 始终如一地平稳地转身面对角色。我认为它会在 void update 中做一些事情,但由于转向的功能目前在 onTriggerEnter 中,这对像我这样的初学者来说有点困惑。

public class helloTrigger : MonoBehaviour
{
    public Animator anim;
    public CharacterController player;
    public Transform Player;

    void Start()
    {
        player = GameObject.FindObjectOfType<CharacterController>();
        anim = GetComponent<Animator>();
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag != "Player") return;

        anim.Play("Hello");
        Vector3 lookAt = Player.position;
        lookAt.y = transform.position.y;
        transform.LookAt(lookAt);
    }

    void OnTriggerExit(Collider other)
    {
        if (other.tag == "Player")
        {
            anim.Play("Idle");
        }
    }
}

最佳答案

如果您希望一个游戏对象平滑地面对另一个游戏对象,请使用 Quaternion.LookRotation 计算目标旋转,然后使用 Quaternion.Lerp 在当前旋转和使用 Quaternion.LookRotation 计算的目标旋转。最后,随着时间的推移进行 lerping。参见 this发布以了解 lerping over rotation 的工作原理。您可以在 Update 函数中执行此操作,但我将在协程函数中使用它,因为它为您提供了更多控制权。

一个简单的Look-at smoothly函数:

IEnumerator LookAtSmoothly(Transform objectToMove, Vector3 worldPosition, float duration)
{
    Quaternion currentRot = objectToMove.rotation;
    Quaternion newRot = Quaternion.LookRotation(worldPosition -
        objectToMove.position, objectToMove.TransformDirection(Vector3.up));

    float counter = 0;
    while (counter < duration)
    {
        counter += Time.deltaTime;
        objectToMove.rotation =
            Quaternion.Lerp(currentRot, newRot, counter / duration);
        yield return null;
    }
}

你的新脚本:

public class helloTrigger : MonoBehaviour
{
    public Animator anim;
    public CharacterController player;
    public Transform Player;
    Coroutine smoothMove = null;

    // Use this for initialization
    void Start()
    {
        player = GameObject.FindObjectOfType<CharacterController>();
        anim = GetComponent<Animator>();
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player")
        {
            anim.Play("Hello");
            LookSmoothly();
        }

    }

    void OnTriggerExit(Collider other)
    {
        if (other.tag == "Player")
        {
            anim.Play("Idle");
        }
    }

    void LookSmoothly()
    {
        float time = 1f;

        Vector3 lookAt = Player.position;
        lookAt.y = transform.position.y;

        //Start new look-at coroutine
        if (smoothMove == null)
            smoothMove = StartCoroutine(LookAtSmoothly(transform, lookAt, time));
        else
        {
            //Stop old one then start new one
            StopCoroutine(smoothMove);
            smoothMove = StartCoroutine(LookAtSmoothly(transform, lookAt, time));
        }
    }

    IEnumerator LookAtSmoothly(Transform objectToMove, Vector3 worldPosition, float duration)
    {
        Quaternion currentRot = objectToMove.rotation;
        Quaternion newRot = Quaternion.LookRotation(worldPosition -
            objectToMove.position, objectToMove.TransformDirection(Vector3.up));

        float counter = 0;
        while (counter < duration)
        {
            counter += Time.deltaTime;
            objectToMove.rotation =
                Quaternion.Lerp(currentRot, newRot, counter / duration);
            yield return null;
        }
    }
}

关于c# - Unity 2018 - NPC 在范围内平滑地转向面对玩家,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51127919/

相关文章:

c# - 在报告中使用 NUnit 显示复杂类型参数的实际值

c# - XML 序列化如何知道将属性放在哪里?

c# - autofac 每个请求类型单个实例

c# - 访问对象方法时的 "< >"运算符是什么?

c# - Unity粒子系统: Change Emitter Velocity with Script

c# - 在 Unity 中将 bool 值从一个场景保存到另一个场景

c# - 用于 C# mono 和 Unity3d 的 Consulo IDE,是否可行?

c# - 在列表/数组中查找元素的更快方法

c# - 使用键查找值,反之亦然

c# - 从谷歌翻译方括号中拆分字符串