gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:52:29 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com