为什么角色旋转太快?

时间:2019-01-09 10:10:57

标签: 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;
    public bool slowDown = false;
    public PlayAnimations playanimation;

    private bool endRot = false;
    private bool endRotation = false;
    private Vector3 center;
    private bool StartWaitingAnim = true;

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

        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);

        for (int i = 0; i < animators.Length; i++)
        {
            animators[2].transform.position = Vector3.MoveTowards(animators[2].transform.position, center, 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].SetBool("Idle", true);

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

                RotateCharacters(2);
            }

            if (!endRot)
            {
                Quaternion goalRotation = Quaternion.Euler(0f, 180f, 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.up, angleThisFrame);
                animators[1].transform.Rotate(Vector3.down, angleThisFrame);

                // We end if we rotated the remaining amount.
                endRot = (angleThisFrame == angleToGoal);
            }
            {
                animators[0].SetBool("Rifle Aiming Idle", true);
                animators[1].SetBool("Rifle Aiming Idle", true);
            }
        }
    }

    private void RotateCharacters(int CharacterIndexToRotate)
    {
        if (!endRotation && waitangimation == true)
        {
            Quaternion goalRotation = Quaternion.Euler(0f, -90f, 0f);
            float angleToGoal = Quaternion.Angle(
                    goalRotation,
                    animators[CharacterIndexToRotate].transform.localRotation);
            float angleThisFrame = Mathf.Min(angleToGoal, 100 * Time.deltaTime);

            animators[CharacterIndexToRotate].transform.Rotate(Vector3.down, angleThisFrame);

            endRotation = (angleThisFrame == angleToGoal);
        }
        else
        {
                animators[2].SetBool("Magic Pack", true);
        }
    }

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

在检查时用于在Update内旋转前两个动画器时:

if (!endRot)

然后动画器0和1都缓慢平滑地旋转。

但是后来我想制作一种旋转方法,并将其称为RotateCharacters 我正在尝试使用这种方法旋转动画器[2],但是他旋转得太快了。 RotateCharacters中的代码与其他动画师的旋转相同。但是动画师[2]仍在快速旋转。

最后,我只希望使用RotateCharacters方法来旋转动画制作器。

1 个答案:

答案 0 :(得分:3)

您以template <class U> T& operator[](U indices) 作为参数调用operator(),而不是循环变量RotateCharacters

因此,您在每次循环迭代时都旋转2