AddExplosionForce.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #if CHRONOS_PLAYMAKER
  2. using HutongGames.PlayMaker;
  3. using UnityEngine;
  4. namespace Chronos.PlayMaker
  5. {
  6. [ActionCategory("Physics (Chronos)")]
  7. [HutongGames.PlayMaker.Tooltip(
  8. "Applies a force to a Game Object that simulates explosion effects. The explosion force will fall off linearly with distance. Hint: Use the Explosion Action instead to apply an explosion force to all objects in a blast radius."
  9. )]
  10. public class AddExplosionForce : ChronosComponentAction<Timeline>
  11. {
  12. [RequiredField]
  13. [CheckForComponent(typeof(Timeline))]
  14. [HutongGames.PlayMaker.Tooltip("The GameObject to add the explosion force to.")]
  15. public FsmOwnerDefault gameObject;
  16. [RequiredField]
  17. [HutongGames.PlayMaker.Tooltip(
  18. "The center of the explosion. Hint: this is often the position returned from a GetCollisionInfo action.")]
  19. public
  20. FsmVector3 center;
  21. [RequiredField]
  22. [HutongGames.PlayMaker.Tooltip("The strength of the explosion.")]
  23. public FsmFloat force;
  24. [RequiredField]
  25. [HutongGames.PlayMaker.Tooltip("The radius of the explosion. Force falls off linearly with distance.")]
  26. public FsmFloat radius;
  27. [HutongGames.PlayMaker.Tooltip(
  28. "Applies the force as if it was applied from beneath the object. This is useful since explosions that throw things up instead of pushing things to the side look cooler. A value of 2 will apply a force as if it is applied from 2 meters below while not changing the actual explosion position."
  29. )]
  30. public FsmFloat upwardsModifier;
  31. [HutongGames.PlayMaker.Tooltip("The type of force to apply. See Unity Physics docs.")]
  32. public ForceMode forceMode;
  33. [HutongGames.PlayMaker.Tooltip("Repeat every frame while the state is active.")]
  34. public bool everyFrame;
  35. public override void Reset()
  36. {
  37. gameObject = null;
  38. center = new FsmVector3 {UseVariable = true};
  39. upwardsModifier = 0f;
  40. forceMode = ForceMode.Force;
  41. everyFrame = false;
  42. }
  43. public override void Awake()
  44. {
  45. Fsm.HandleFixedUpdate = true;
  46. }
  47. public override void OnEnter()
  48. {
  49. DoAddExplosionForce();
  50. if (!everyFrame)
  51. {
  52. Finish();
  53. }
  54. }
  55. public override void OnFixedUpdate()
  56. {
  57. DoAddExplosionForce();
  58. }
  59. private void DoAddExplosionForce()
  60. {
  61. var go = gameObject.OwnerOption == OwnerDefaultOption.UseOwner ? Owner : gameObject.GameObject.Value;
  62. if (center == null || !UpdateCache(go))
  63. {
  64. return;
  65. }
  66. timeline.rigidbody.AddExplosionForce(force.Value, center.Value, radius.Value, upwardsModifier.Value, forceMode);
  67. }
  68. }
  69. }
  70. #endif