为什么角色从不旋转?

时间:2019-01-10 18:29:12

标签: c# unity3d

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

public class AnimatorController : MonoBehaviour
{
    [Header("Animators")]
    public Animator[] animators;
    [Space(5)]
    [Header("Movement Settings")]
    public Transform target;
    public float movingSpeed = 1f;
    public bool slowDown = false;
    [Space(5)]
    [Header("Rotation Settings")]
    public float rotationSpeed;

    private bool endRotation = false;
    private Vector3 targetCenter;
    private bool startWaitingAnim = true;

    // Use this for initialization
    void Start()
    {
        targetCenter = target.GetComponent<Renderer>().bounds.center;

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

    // Update is called once per frame
    void Update()
    {
        float distanceFromTarget = Vector3.Distance(animators[2].transform.position, target.position);
        animators[2].transform.position = Vector3.MoveTowards(animators[2].transform.position, targetCenter, 0);

        if (slowDown)
        {
            if (distanceFromTarget < 10)
            {
                float speed = (distanceFromTarget / 10) / 1;
                for (int i = 0; i < animators.Length; i++)
                {
                    animators[i].SetFloat("Walking Speed", speed);
                }
            }
        }

        if (distanceFromTarget < 5f)
        {
            for (int i = 0; i < animators.Length; i++)
            {
                //animators[i].SetFloat("Walking Speed", 0);
                animators[i].SetBool("Idle", true);

                if (startWaitingAnim == true)
                {
                    StartCoroutine(WaitForAnimation());
                    startWaitingAnim = false;
                }
            }

            if (waitinganimation == true)
            {
                RotateAll(new Quaternion(0, -90, 0, 0),
                    Vector3.down, "Magic Pack", animators[2]);
            }

            RotateAll(new Quaternion(0, 180, 0, 0),
                    Vector3.up, "Rifle Aiming Idle",  animators[0]);

            RotateAll(new Quaternion(0, 180, 0, 0),
                    Vector3.down, "Rifle Aiming Idle", animators[1]);
        }
    }

    private void ApplyRotation(Quaternion goalRotation,
         Vector3 axis, string AnimationName, Animator anim)
    {
        if (!endRotation)
        {
            float angleToGoal = Quaternion.Angle(
                    goalRotation,
                    anim.transform.localRotation);
            float angleThisFrame = Mathf.Min(angleToGoal, 100 * Time.deltaTime);

            anim.transform.Rotate(axis, angleThisFrame);

            endRotation = Mathf.Approximately(angleThisFrame, angleToGoal);
        }
        else
        {
            anim.SetBool(AnimationName, true);
        }
    }

    void RotateAll(Quaternion rotation,
        Vector3 axis,
        string AnimationName, params Animator[] anims)
    {
        foreach (var anim in anims)
            ApplyRotation(rotation, axis, AnimationName, anim); // However you want to actually apply the rotation
    }

    bool waitinganimation = false;
    IEnumerator WaitForAnimation()
    {
        yield return new WaitForSeconds(3);
        waitinganimation = true;
    }
}

第一个问题是动画师没有一个在旋转。 没有错误或异常,它只是没有旋转。

我检查了断点是否进入内部

if (!endRotation)

然后转到其他位置并播放动画 播放动画很好,但之前没有旋转。

这个想法是动画器中的每个动画器将旋转到另一个轴/角度/方向。同时或取决于其他条件,例如动画师[2],应首先等待waitinganimation成立。

另一个问题是动画制作者两行[0]和[1]:

RotateAll(new Quaternion(0, 180, 0, 0),
                        Vector3.up, "Rifle Aiming Idle",  animators[0]);

                RotateAll(new Quaternion(0, 180, 0, 0),
                        Vector3.down, "Rifle Aiming Idle", animators[1]);

两者旋转相同的角度,但旋转的轴线不同。 也许有一种方法可以扩展动画参数:

params Animator[] anims

所以我将能够写类似的东西:

RotateAll(new Quaternion(0, 180, 0, 0),
           "Rifle Aiming Idle",  animators[0, Vector3.Up], animators[1, Vector3.Down]);

为每个轴更改添加一行。

1 个答案:

答案 0 :(得分:2)

使用欧拉角生成Quaternion时,请使用Quaternion.Euler

通常,除非您对四元数数学非常熟悉,否则不要使用Quaternion构造函数!

在特定情况下,请使用Quaternion.Euler(0f, 180f, 0f)代替new Quaternion(0, 180, 0, 0),并使用Quaternion.Euler(0f, -90f, 0f)代替new Quaternion(0, -90, 0, 0)