Почему персонаж вращается слишком быстро? - PullRequest
0 голосов
/ 09 января 2019
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;
    }
}

При использовании для поворота первых двух аниматоров внутри Обновления при проверке:

if (!endRot)

Тогда оба аниматора 0 и 1 вращаются медленно плавно.

Но тогда я хотел создать метод для вращения и назвал его RotateCharacters Этим способом я пытаюсь вращать аниматоры [2], но он вращается очень быстро, слишком быстро. И код внутри RotateCharacters такой же, как и для других аниматоров. Но все же аниматоры [2] вращаются очень быстро.

В конце я хочу остановиться только на методе RotateCharacters для вращения аниматоров.

1 Ответ

0 голосов
/ 09 января 2019

Вы вызываете RotateCharacters с 2 в качестве параметра, а не с переменной цикла i.

Таким образом, вы вращаете animators[2] в каждой итерации цикла.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...