Daniel Lip
Daniel Lip

Reputation: 11317

Why the character is rotating too fast?

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

When using to rotate the first two animators inside Update when checking:

if (!endRot)

Then both animators 0 and 1 are rotating slowly smooth.

But then I wanted to make a method for rotating and called it RotateCharacters This this method I'm trying to rotate animators[2] but he is rotating very fast too fast. And the code inside RotateCharacters is the same for rotating the other animators. But still animators[2] is rotating very fast.

In the end I will want to stay only with the method RotateCharacters for rotating the animators.

Upvotes: 0

Views: 92

Answers (1)

Piflik
Piflik

Reputation: 351

You call RotateCharacters with 2 as parameter as opposed to the loop variable i.

So you rotate animators[2] in every loop-iteration.

Upvotes: 3

Related Questions