CharacterAnimationSimple.cs 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion.Demos {
  4. /// <summary>
  5. /// Contols animation for a simple Mecanim character
  6. /// </summary>
  7. public class CharacterAnimationSimple: CharacterAnimationBase {
  8. public CharacterThirdPerson characterController;
  9. public float pivotOffset; // Offset of the rotating pivot point from the root
  10. public AnimationCurve moveSpeed; // The moving speed relative to input forward
  11. private Animator animator;
  12. protected override void Start() {
  13. base.Start();
  14. animator = GetComponentInChildren<Animator>();
  15. }
  16. public override Vector3 GetPivotPoint() {
  17. if (pivotOffset == 0) return transform.position;
  18. return transform.position + transform.forward * pivotOffset;
  19. }
  20. // Update the Animator with the current state of the character controller
  21. void Update() {
  22. float speed = moveSpeed.Evaluate(characterController.animState.moveDirection.z);
  23. // Locomotion
  24. animator.SetFloat("Speed", speed);
  25. // Movement
  26. characterController.Move(characterController.transform.forward * Time.deltaTime * speed, Quaternion.identity);
  27. }
  28. }
  29. }