MonoBehaviour.OnAnimatorMove()
Описание
Обратный вызов для обработки движений анимации для изменения движения корня.
Этот обратный вызов будет вызываться в каждом кадре после того, как машина состояний и анимации будут вычислены, но до OnAnimatorIK.
Дополнительные ресурсы: Корневое движение.
// Attach this script to your character GameObject that has an Animator and optionally a Rigidbody. // Enable "Apply Root Motion" on the Animator. Adjust rootMotionMultiplier to speed up or slow down root motion.
using UnityEngine;
[RequireComponent(typeof(Animator))] public class RootMotionModifier : MonoBehaviour { public float rootMotionMultiplier = 2.0f; // Scale the root motion
private Animator animator; private Rigidbody rb;
void Awake() { animator = GetComponent<Animator>(); rb = GetComponent<Rigidbody>(); }
void OnAnimatorMove() { // Only modify root motion if we have a Rigidbody if (rb != null) { // Get the deltaPosition from the Animator Vector3 deltaPosition = animator.deltaPosition * rootMotionMultiplier; // Preserve vertical velocity (for example, gravity) Vector3 velocity = deltaPosition / Time.deltaTime; velocity.y = rb.velocity.y; rb.velocity = velocity;
// Optionally apply root rotation rb.MoveRotation(rb.rotation * animator.deltaRotation); } else { // If no Rigidbody, just move the transform transform.position += animator.deltaPosition * rootMotionMultiplier; transform.rotation *= animator.deltaRotation; } } }