ExplosionDemo.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using UnityEngine;
  2. using System.Collections;
  3. using RootMotion.FinalIK;
  4. namespace RootMotion.Demos {
  5. /// <summary>
  6. /// Demo of exploding a viking using FBBIK
  7. /// </summary>
  8. public class ExplosionDemo : MonoBehaviour {
  9. public SimpleLocomotion character; // Reference to the SimpleLocomotion component
  10. public float forceMlp = 1f; // Explosion force
  11. public float upForce = 1f; // Explosion up forve
  12. public float weightFalloffSpeed = 1f; // The speed of explosion falloff
  13. public AnimationCurve weightFalloff; // Explosion weight falloff
  14. public AnimationCurve explosionForceByDistance; // The force of the explosion relative to character distance to the bomb
  15. public AnimationCurve scale; // Scaling the bomb GameObject with the explosion
  16. private float weight = 0f;
  17. private Vector3 defaultScale = Vector3.one;
  18. private Rigidbody r;
  19. private FullBodyBipedIK ik;
  20. void Start() {
  21. // Storing the default scale of the bomb
  22. defaultScale = transform.localScale;
  23. r = character.GetComponent<Rigidbody>();
  24. ik = character.GetComponent<FullBodyBipedIK>();
  25. }
  26. // Update is called once per frame
  27. void Update () {
  28. weight = Mathf.Clamp(weight - Time.deltaTime * weightFalloffSpeed, 0f, 1f);
  29. // Exploding the bomb
  30. if (Input.GetKeyDown(KeyCode.E) && character.isGrounded) {
  31. // Set FBBIK weight to 1
  32. ik.solver.IKPositionWeight = 1f;
  33. // Set limb effector positions to where they are at the momemt
  34. ik.solver.leftHandEffector.position = ik.solver.leftHandEffector.bone.position;
  35. ik.solver.rightHandEffector.position = ik.solver.rightHandEffector.bone.position;
  36. ik.solver.leftFootEffector.position = ik.solver.leftFootEffector.bone.position;
  37. ik.solver.rightFootEffector.position = ik.solver.rightFootEffector.bone.position;
  38. weight = 1f;
  39. // Add explosion force to the character rigidbody
  40. Vector3 direction = r.position - transform.position;
  41. direction.y = 0f;
  42. float explosionForce = explosionForceByDistance.Evaluate(direction.magnitude);
  43. r.velocity = (direction.normalized + (Vector3.up * upForce)) * explosionForce * forceMlp;
  44. }
  45. if (weight < 0.5f && character.isGrounded) {
  46. weight = Mathf.Clamp(weight - Time.deltaTime * 3f, 0f, 1f);
  47. }
  48. // Set effector weights
  49. SetEffectorWeights(weightFalloff.Evaluate(weight));
  50. // Set bomb scale
  51. transform.localScale = scale.Evaluate(weight) * defaultScale;
  52. }
  53. // Set FBBIK limb end-effector weights to value
  54. private void SetEffectorWeights(float w) {
  55. ik.solver.leftHandEffector.positionWeight = w;
  56. ik.solver.rightHandEffector.positionWeight = w;
  57. ik.solver.leftFootEffector.positionWeight = w;
  58. ik.solver.rightFootEffector.positionWeight = w;
  59. }
  60. }
  61. }