// Animancer // https://kybernetik.com.au/animancer // Copyright 2018-2023 Kybernetik // #pragma warning disable CS0649 // Field is never assigned to, and will always have its default value. using Animancer.Units; using System; using UnityEngine; namespace Animancer.Examples.Locomotion { /// Demonstrates how you can use Root Motion for some animations but not others. /// Root Motion /// https://kybernetik.com.au/animancer/api/Animancer.Examples.Locomotion/RootMotion /// [AddComponentMenu(Strings.ExamplesMenuPrefix + "Locomotion - Root Motion")] [HelpURL(Strings.DocsURLs.ExampleAPIDocumentation + nameof(Locomotion) + "/" + nameof(RootMotion))] public sealed class RootMotion : MonoBehaviour { /************************************************************************************************************************/ /// A with an toggle. [Serializable] public class MotionTransition : ClipTransition { /************************************************************************************************************************/ [SerializeField, Tooltip("Should Root Motion be enabled when this animation plays?")] private bool _ApplyRootMotion; /************************************************************************************************************************/ public override void Apply(AnimancerState state) { base.Apply(state); state.Root.Component.Animator.applyRootMotion = _ApplyRootMotion; } /************************************************************************************************************************/ } /************************************************************************************************************************/ [SerializeField] private AnimancerComponent _Animancer; [SerializeField, Meters] private float _MaxDistance; [SerializeField] private MotionTransition[] _Animations; private Vector3 _Start; /************************************************************************************************************************/ private void OnEnable() { _Start = transform.position; Play(0); } /************************************************************************************************************************/ /// Plays the animation at the specified `index` in the array. /// This method is called by UI Buttons. public void Play(int index) { _Animancer.Play(_Animations[index]); } /************************************************************************************************************************/ /// Teleports this object back to its starting location if it moves too far away. private void FixedUpdate() { if (Vector3.Distance(_Start, transform.position) > _MaxDistance) transform.position = _Start; } /************************************************************************************************************************/ } }