c# - 一旦角色在 y 上变为 0,我该如何停止角色旋转?

标签 c# unity3d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimatorController : MonoBehaviour
{
    public Animator[] animators;
    public Transform target;
    public float speed = 1f;
    public float rotationSpeed;

    private bool endRot = false;

    // Use this for initialization
    void Start()
    {
        for (int i = 0; i < animators.Length; i++)
        {
            animators[i].SetFloat("Walking Speed", speed);
        }
    }

    // Update is called once per frame
    void Update()
    {
        float distanceFromTarget = Vector3.Distance(animators[2].transform.position, target.position);
        if (distanceFromTarget < 15)
        {
            float speed = (distanceFromTarget / 15) / 1;
            for (int i = 0; i < animators.Length; i++)
            {
                animators[i].SetFloat("Walking Speed", speed);
            }
        }

        if (distanceFromTarget < 2f)
        {
            for (int i = 0; i < animators.Length; i++)
            {
                animators[i].SetFloat("Walking Speed", 0);
            }

            if (animators[0].transform.localRotation.y != 0 && endRot == false)
            {
                animators[0].transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime);
                animators[1].transform.Rotate(Vector3.up, -rotationSpeed * Time.deltaTime);
            }

            if (animators[0].transform.localRotation.y == 0)
                endRot = true;
        }
    }
}

两个字符都以 localRotation 的欧拉角 (0f, 180f, 0f) 开头。 我希望它们在 Y 上旋转到 0,然后停止。尝试使用标志,但我猜比较 float 是错误的。我应该为此使用 StartCoroutine 吗?

我尝试使用时间轴制作动画剪辑,但是一旦我创建了一个新的动画轨道并由于某种原因拖入其中一个角色,它就会改变角色的位置并且它的武器没有附加到他身上。不确定为什么时间轴会发生这种情况。

最佳答案

您需要限制旋转,以免在单个旋转框架内出现过冲。这是一种方法。

设置目标轮换:

Quaternion goalRotation = Quaternion.Euler(0f,0f,0f); // or Quaternion.identity

找出第一个变换的旋转与旋转的度数之间的距离:

float angleToGoal = Quaternion.Angle(goalRotation, animators[0].transform.localRotation);

确定哪个较小:由速度给出的角度 * 增量时间剩余的角度,以确定您应该将此框架旋转多远。 这是重要的夹紧部分:

float angleThisFrame = Mathf.Min(angleToGoal, rotationSpeed * Time.deltaTime);

然后,旋转该数量。如果您通过 angleToGoal 进行旋转,则设置 endRot = true;

这看起来像这样:

if (distanceFromTarget < 2f)
{
    for (int i = 0; i < animators.Length; i++)
    {
        animators[i].SetFloat("Walking Speed", 0);
    }

    if (!endRot)
    {
        Quaternion goalRotation = Quaternion.Euler(0f,0f,0f);
        float angleToGoal = Quaternion.Angle(
                goalRotation,
                animators[0].transform.localRotation);         
        float angleThisFrame = Mathf.Min(angleToGoal, rotationSpeed * Time.deltaTime);   

        // use axis of Vector3.down to keep angles positive for ease of use
        animators[0].transform.Rotate(Vector3.down, angleThisFrame);
        animators[1].transform.Rotate(Vector3.up, angleThisFrame);

        // We end if we rotated the remaining amount.
        endRot = (angleThisFrame == angleToGoal);
    }
}

关于c# - 一旦角色在 y 上变为 0,我该如何停止角色旋转?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53889656/

相关文章:

c# - 调试/检查 XML 序列化的工具

c# - 添加 Azure AD 身份验证时出现 CORS 错误

c# - 微软应用中心 : How get list of users with session duration?

c# - .NET 4.x 相对于 .NET 3.5 更改了小数点 - 如何修复? (统一)

swift - ARKit——适应光照条件

c# - javascript 版本(asp-append-version)如何在 ASP.NET Core MVC 中工作?

c# - 良好的远程应用程序日志记录/监控软件

c# - 如何使用 C# 配置组策略?

c# - udpclient 仅在本地接收广播(c#、Unity)

c# - lambda 表达式或 get_property 函数在 C# 中的实际作用是什么?