- Home /
 
               Question by 
               Chocolade · Jan 11, 2019 at 08:38 AM · 
                c#scripting problemscript.  
              
 
              How can I rotate all the objects ? One of them is never rotating
 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].SetBool("Idle", true);
 
                 if (startWaitingAnim == true)
                 {
                     StartCoroutine(WaitForAnimation());
                     startWaitingAnim = false;
                 }
             }
 
             if (waitinganimation == true)
             {
                 RotateAll(Quaternion.Euler(0, -90, 0),
                     Vector3.up, "Magic Pack", animators[2]);
             }
 
             RotateAll(Quaternion.Euler(0, 0, 0),
                     Vector3.down, "Rifle Aiming Idle", animators[0]);
 
             RotateAll(Quaternion.Euler(0, 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;
     }
 }
 
The problem is in the method ApplyRotation. The variable flag bool endRotation will be true after animators[0] and [1] finished rotating. And then animators[2] will never rotate since endRotation is already true.
animators[2] is waiting for the flag waitinganimation to be true and then it should start rotating while the other two rotating before. How can I solve the problem with the endRotation being true already ?
In this case I have 3 objects to rotate animators[0] and animators[1] and animators[2] in the future I might have more objects that will rotate some at the same time some in other times.
               Comment
              
 
               
              Your answer
 
 
             