MotionAbsorbCharacter.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace RootMotion.Demos {
  4. /// <summary>
  5. /// Motion Absorb demo character controller.
  6. /// </summary>
  7. public class MotionAbsorbCharacter : MonoBehaviour {
  8. public Animator animator;
  9. public MotionAbsorb motionAbsorb;
  10. public Transform cube; // The cube we are hitting
  11. public float cubeRandomPosition = 0.1f; // Randomizing cube position after each hit
  12. public AnimationCurve motionAbsorbWeight;
  13. private Vector3 cubeDefaultPosition;
  14. private AnimatorStateInfo info;
  15. private Rigidbody cubeRigidbody;
  16. void Start() {
  17. // Storing the default position of the cube
  18. cubeDefaultPosition = cube.position;
  19. cubeRigidbody = cube.GetComponent<Rigidbody>();
  20. }
  21. void Update () {
  22. // Set motion absorb weight
  23. //motionAbsorb.weight = animator.GetFloat("MotionAbsorbWeight"); // NB! Using Mecanim curves is PRO only
  24. // Using an animation curve so it works with Unity Free as well
  25. info = animator.GetCurrentAnimatorStateInfo(0);
  26. motionAbsorb.weight = motionAbsorbWeight.Evaluate(info.normalizedTime - (int)info.normalizedTime);
  27. }
  28. // Mecanim event
  29. void SwingStart() {
  30. // Reset the cube
  31. cubeRigidbody.MovePosition(cubeDefaultPosition + UnityEngine.Random.insideUnitSphere * cubeRandomPosition);
  32. cubeRigidbody.MoveRotation(Quaternion.identity);
  33. cubeRigidbody.velocity = Vector3.zero;
  34. cubeRigidbody.angularVelocity = Vector3.zero;
  35. }
  36. }
  37. }