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], которые должны сначала подождать, пока ожидание анимации сбудется.
Другая проблема связана с двумя строками аниматоров [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]);
Вместо этого добавляется линия для каждого изменения оси.