CharacterAnimationThirdPerson.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion.Demos {
  4. /// <summary>
  5. /// Contols animation for a third person person controller.
  6. /// </summary>
  7. public class CharacterAnimationThirdPerson: CharacterAnimationBase {
  8. public CharacterThirdPerson characterController;
  9. [SerializeField] float turnSensitivity = 0.2f; // Animator turning sensitivity
  10. [SerializeField] float turnSpeed = 5f; // Animator turning interpolation speed
  11. [SerializeField] float runCycleLegOffset = 0.2f; // The offset of leg positions in the running cycle
  12. [Range(0.1f,3f)] [SerializeField] float animSpeedMultiplier = 1; // How much the animation of the character will be multiplied by
  13. protected Animator animator;
  14. private Vector3 lastForward;
  15. private const string groundedDirectional = "Grounded Directional", groundedStrafe = "Grounded Strafe";
  16. private float deltaAngle;
  17. private float jumpLeg;
  18. private bool lastJump;
  19. protected override void Start() {
  20. base.Start();
  21. animator = GetComponent<Animator>();
  22. lastForward = transform.forward;
  23. }
  24. public override Vector3 GetPivotPoint() {
  25. return animator.pivotPosition;
  26. }
  27. // Is the Animator playing the grounded animations?
  28. public override bool animationGrounded {
  29. get {
  30. return animator.GetCurrentAnimatorStateInfo(0).IsName(groundedDirectional) || animator.GetCurrentAnimatorStateInfo(0).IsName(groundedStrafe);
  31. }
  32. }
  33. // Update the Animator with the current state of the character controller
  34. protected virtual void Update() {
  35. if (Time.deltaTime == 0f) return;
  36. animatePhysics = animator.updateMode == AnimatorUpdateMode.AnimatePhysics;
  37. // Jumping
  38. if (characterController.animState.jump) {
  39. if (!lastJump)
  40. {
  41. float runCycle = Mathf.Repeat(animator.GetCurrentAnimatorStateInfo(0).normalizedTime + runCycleLegOffset, 1);
  42. float jumpLeg = (runCycle < 0.5f ? 1 : -1) * characterController.animState.moveDirection.z;
  43. animator.SetFloat("JumpLeg", jumpLeg);
  44. }
  45. }
  46. lastJump = characterController.animState.jump;
  47. // Calculate the angular delta in character rotation
  48. float angle = -GetAngleFromForward(lastForward) - deltaAngle;
  49. deltaAngle = 0f;
  50. lastForward = transform.forward;
  51. angle *= turnSensitivity * 0.01f;
  52. angle = Mathf.Clamp(angle / Time.deltaTime, -1f, 1f);
  53. // Update Animator params
  54. animator.SetFloat("Turn", Mathf.Lerp(animator.GetFloat("Turn"), angle, Time.deltaTime * turnSpeed));
  55. animator.SetFloat("Forward", characterController.animState.moveDirection.z);
  56. animator.SetFloat("Right", characterController.animState.moveDirection.x);
  57. animator.SetBool("Crouch", characterController.animState.crouch);
  58. animator.SetBool("OnGround", characterController.animState.onGround);
  59. animator.SetBool("IsStrafing", characterController.animState.isStrafing);
  60. if (!characterController.animState.onGround) {
  61. animator.SetFloat ("Jump", characterController.animState.yVelocity);
  62. }
  63. if (characterController.doubleJumpEnabled) animator.SetBool("DoubleJump", characterController.animState.doubleJump);
  64. characterController.animState.doubleJump = false;
  65. // the anim speed multiplier allows the overall speed of walking/running to be tweaked in the inspector
  66. if (characterController.animState.onGround && characterController.animState.moveDirection.z > 0f) {
  67. animator.speed = animSpeedMultiplier;
  68. } else {
  69. // but we don't want to use that while airborne
  70. animator.speed = 1;
  71. }
  72. }
  73. // Call OnAnimatorMove manually on the character controller because it doesn't have the Animator component
  74. void OnAnimatorMove() {
  75. // For not using root rotation in Turn value calculation
  76. Vector3 f = animator.deltaRotation * Vector3.forward;
  77. deltaAngle += Mathf.Atan2(f.x, f.z) * Mathf.Rad2Deg;
  78. characterController.Move(animator.deltaPosition, animator.deltaRotation);
  79. }
  80. }
  81. }