0

我现在有一个设置,其中我有一个 3D 对象,该对象内部有一个空对象。当您按下一个按钮时,相机跟随的空物体会旋转 90 度。如果您按下另一个按钮,它将向另一个方向旋转 90 度。结果是相机可以在完成旋转之前围绕对象旋转 4 次。

目前它运作良好,但我试图弄清楚如何为动画添加一些缓动,使其看起来不那么粗糙。我对在动画中使用曲线有一点了解,但我不确定如何将其应用于代码(或者它是否是最好的做事方式)

    public InputMaster controls;
    private bool isSpinning;
    private float rotationSpeed = 0.3f;

    IEnumerator RotateMe(Vector3 byangles, float intime)
    {
        var fromangle = transform.rotation;
        var toangle = Quaternion.Euler(transform.eulerAngles + byangles);
        for (var t = 0f; t < 1; t += Time.deltaTime / intime)
        {
            transform.rotation = Quaternion.Slerp(fromangle, toangle, t);
            yield return null;
            transform.rotation = toangle;
        }

            Debug.Log("finished rotation");
            isSpinning = false;
            Debug.Log("isSpinning now false");
    }


上面的代码是我创建一个协程的地方,它说明了它将如何转换。让我有点困惑的一件事是,如果我没有说 的线,transform.rotation = toangle;旋转就会出现在 89.5 度之类的地方,如果你多次这样做,它会偏离几度。不知道为什么会这样。

    void Rotation(float amount)
    {
        Debug.Log("rotation number is " + amount);

        float holdingRotate = controls.Player.Camera.ReadValue<float>();

        if (holdingRotate == 1 && isSpinning == false)
        {
            isSpinning = true;
            Debug.Log("isSpinning now true");
            StartCoroutine(RotateMe(Vector3.up * 90, rotationSpeed));

        }
        else if (holdingRotate == -1 && isSpinning == false)
        {
            isSpinning = true;
            Debug.Log("isSpinning now true");
            StartCoroutine(RotateMe(Vector3.up * -90, rotationSpeed));
        }
    }

这部分是调用动画的地方。非常感谢任何帮助。

4

3 回答 3

2

看看@immersive 说的动画曲线。

只需将其添加到代码中,您就可以轻松地在检查器中定义自己的曲线:

public AnimationCurve curve;

检查器中的曲线

然后,您可以使用AnimationCurve.Evaluate 对曲线进行采样。

另一种方法是使用 AssetStore/Package Manager 中的预制库,例如 DOTween 或 LeanTween。

于 2020-11-02T13:56:01.423 回答
0
for (var t = 0f; t < 1; t += Time.deltaTime / intime)
{
    transform.rotation = Quaternion.Slerp(fromangle, toangle, t);
    yield return null;
}
transform.rotation = toangle;

因为t不会等于1。

您只需要在循环后将旋转设置为目标。

于 2020-11-02T03:36:06.453 回答
0

内联评论:

public class SmoothRotator : MonoBehaviour
{

    // Animation curve holds the 'lerp factor' from starting angle to final angle over time
    // (Y axis is blend factor, X axis is normalized 'time' factor)
    public AnimationCurve Ease = AnimationCurve.EaseInOut(0, 0, 1, 1);
    public float Duration = 1f;

    private IEnumerator ActiveCoroutine = null;

    public void RotateToward(Quaternion targetAngle) {

        // If there's a Coroutine to stop, stop it.
        if (ActiveCoroutine != null)
            StopCoroutine(ActiveCoroutine);

        // Start new Coroutine and cache the IEnumerator in case we need to interrupt it.
        StartCoroutine(ActiveCoroutine = Rotator(targetAngle));
    }

    public IEnumerator Rotator(Quaternion targetAngle) {

        // Store starting angle
        var fromAngle = transform.rotation;

        // Accumulator for Time.deltaTime
        var age = 0f; 

        while (age < 1f) {
            // normalize time (scale to "percentage complete") and clamp it
            var normalisedTime = Mathf.Clamp01(age / Duration);

            // Pull lerp factor from AnimationCurve
            var lerpFactor = Ease.Evaluate(normalisedTime);

            // Set rotation
            transform.rotation = Quaternion.Slerp(fromAngle, targetAngle, lerpFactor);

            // Wait for next frame
            yield return null;
            // Update age with new frame's deltaTime
            age += Time.deltaTime;
        }

        // Animation complete, force true value
        transform.rotation = targetAngle;

        // Housekeeping, clear ActiveCoroutine
        ActiveCoroutine = null;
    }

}
于 2020-11-02T13:49:46.407 回答