Advertisement
PJPalomaki

PlayMaker AddForce

Mar 10th, 2016
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.86 KB | None | 0 0
  1. // (c) Copyright HutongGames, LLC 2010-2013. All rights reserved.
  2.  
  3. using UnityEngine;
  4.  
  5. namespace HutongGames.PlayMaker.Actions
  6. {
  7.     [ActionCategory(ActionCategory.Physics)]
  8.     [Tooltip("Adds a force to a Game Object. Use Vector3 variable and/or Float variables for each axis.")]
  9.     public class AddForce : ComponentAction<Rigidbody>
  10.     {
  11.         [RequiredField]
  12.         [CheckForComponent(typeof(Rigidbody))]
  13.         [Tooltip("The GameObject to apply the force to.")]
  14.         public FsmOwnerDefault gameObject;
  15.        
  16.         [UIHint(UIHint.Variable)]
  17.         [Tooltip("Optionally apply the force at a position on the object. This will also add some torque. The position is often returned from MousePick or GetCollisionInfo actions.")]
  18.         public FsmVector3 atPosition;
  19.        
  20.         [UIHint(UIHint.Variable)]
  21.         [Tooltip("A Vector3 force to add. Optionally override any axis with the X, Y, Z parameters.")]
  22.         public FsmVector3 vector;
  23.  
  24.         [Tooltip("Force along the X axis. To leave unchanged, set to 'None'.")]
  25.         public FsmFloat x;
  26.  
  27.         [Tooltip("Force along the Y axis. To leave unchanged, set to 'None'.")]
  28.         public FsmFloat y;
  29.  
  30.         [Tooltip("Force along the Z axis. To leave unchanged, set to 'None'.")]
  31.         public FsmFloat z;
  32.  
  33.         [Tooltip("Apply the force in world or local space.")]
  34.         public Space space;
  35.  
  36.         [Tooltip("The type of force to apply. See Unity Physics docs.")]
  37.         public ForceMode forceMode;
  38.  
  39.         [Tooltip("Repeat every frame while the state is active.")]
  40.         public bool everyFrame;
  41.  
  42.         public override void Reset()
  43.         {
  44.             gameObject = null;
  45.             atPosition = new FsmVector3 { UseVariable = true };
  46.             vector = null;
  47.             // default axis to variable dropdown with None selected.
  48.             x = new FsmFloat { UseVariable = true };
  49.             y = new FsmFloat { UseVariable = true };
  50.             z = new FsmFloat { UseVariable = true };
  51.             space = Space.World;
  52.             forceMode = ForceMode.Force;
  53.             everyFrame = false;
  54.         }
  55.  
  56.         public override void OnPreprocess()
  57.         {
  58.             Fsm.HandleFixedUpdate = true;
  59.         }
  60.  
  61.         public override void OnEnter()
  62.         {
  63.             DoAddForce();
  64.            
  65.             if (!everyFrame)
  66.             {
  67.                 Finish();
  68.             }      
  69.         }
  70.  
  71.         public override void OnFixedUpdate()
  72.         {
  73.             DoAddForce();
  74.         }
  75.  
  76.         void DoAddForce()
  77.         {
  78.             var go = Fsm.GetOwnerDefaultTarget(gameObject);
  79.             if (!UpdateCache(go))
  80.             {
  81.                 return;
  82.             }
  83.  
  84.             var force = vector.IsNone ? new Vector3() : vector.Value;
  85.            
  86.             // override any axis
  87.  
  88.             if (!x.IsNone) force.x = x.Value;
  89.             if (!y.IsNone) force.y = y.Value;
  90.             if (!z.IsNone) force.z = z.Value;
  91.                    
  92.             // apply force         
  93.                
  94.             if (space == Space.World)
  95.             {
  96.                 if (!atPosition.IsNone)
  97.                 {
  98.                     rigidbody.AddForceAtPosition(force, atPosition.Value, forceMode);
  99.                 }
  100.                 else
  101.                 {
  102.                     rigidbody.AddForce(force, forceMode);
  103.                 }
  104.             }
  105.             else
  106.             {
  107.                 rigidbody.AddRelativeForce(force,forceMode);
  108.             }
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement