playMaker

Author Topic: Several Character Motor Functions  (Read 6745 times)

Vanlande

  • Playmaker Newbie
  • *
  • Posts: 5
Several Character Motor Functions
« on: April 14, 2012, 05:33:20 PM »
Hey mates. I bring to you today a few simple Actions that I made in order to expose a few function already present in the Character Motor script. They are very simple Actions that store bools if a state is true or not, such as Jumping, Sliding or IsGrounded. In fact, they are basically taken from the ControllerIsGrounded action.
They rather help me a lot when I made a FSM that blends between animations, since it's important to me to know if the character is jumping or sliding or other things. Hope they help you out. I've just started to use PlayMaker this week, so if these actions can be achieved with standard actions I apologize for this post  ;)

Cheers,
Ed.

Action: Motor Is Jumping. Bool that tells us if the character motor is jumping. Can be store in a bool variable.

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Character)]
[Tooltip("Tests if a Character Motor on a Game Object is running or not.")]
public class MotorIsJumping : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(CharacterMotor))]
[Tooltip("The GameObject to check.")]
public FsmOwnerDefault gameObject;

[Tooltip("Event to send if jumping.")]
public FsmEvent trueEvent;

[Tooltip("Event to send if not jumping.")]
public FsmEvent falseEvent;

[Tooltip("Sore the result in a bool variable.")]
[UIHint(UIHint.Variable)]
public FsmBool storeResult;

[Tooltip("Repeate every frame.")]
public bool everyFrame;

private GameObject previousGo; // remember so we can get new controller only when it changes.
CharacterMotor targetMotor;

public override void Reset()
{
gameObject = null;
trueEvent = null;
falseEvent = null;
storeResult = null;
everyFrame = false;
}

public override void OnEnter()
{
DoMotorIsJumping();

if (!everyFrame)
{
Finish();
}
}

public override void OnUpdate()
{
DoMotorIsJumping();
}

void DoMotorIsJumping()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

if (go != previousGo)
{
targetMotor = go.GetComponent<CharacterMotor>();
previousGo = go;
}

if (targetMotor == null) return;

var onJump = targetMotor.IsJumping();

storeResult.Value = onJump;

Fsm.Event(onJump ? trueEvent : falseEvent);
}
}
}


Action: Motor Is Sliding. Bool that tells us if the character motor is sliding. Can be store in a bool variable.

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Character)]
[Tooltip("Tests if a Character Motor on a Game Object is sliding or not.")]
public class MotorIsSliding : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(CharacterMotor))]
[Tooltip("The GameObject to check.")]
public FsmOwnerDefault gameObject;

[Tooltip("Event to send if sliding.")]
public FsmEvent trueEvent;

[Tooltip("Event to send if not sliding.")]
public FsmEvent falseEvent;

[Tooltip("Sore the result in a bool variable.")]
[UIHint(UIHint.Variable)]
public FsmBool storeResult;

[Tooltip("Repeate every frame.")]
public bool everyFrame;

private GameObject previousGo; // remember so we can get new controller only when it changes.
CharacterMotor targetMotor;

public override void Reset()
{
gameObject = null;
trueEvent = null;
falseEvent = null;
storeResult = null;
everyFrame = false;
}

public override void OnEnter()
{
DoMotorIsSliding();

if (!everyFrame)
{
Finish();
}
}

public override void OnUpdate()
{
DoMotorIsSliding();
}

void DoMotorIsSliding()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

if (go != previousGo)
{
targetMotor = go.GetComponent<CharacterMotor>();
previousGo = go;
}

if (targetMotor == null) return;

var onSlide = targetMotor.IsSliding();

storeResult.Value = onSlide;

Fsm.Event(onSlide ? trueEvent : falseEvent);
}
}
}


Action: Motor Is Grounded. Same as controller is grounded, except interfaces directly with character motor.

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Character)]
[Tooltip("Tests if a Character Motor on a Game Object was touching the ground during the last move.")]
public class MotorIsGrounded : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(CharacterMotor))]
[Tooltip("The GameObject to check.")]
public FsmOwnerDefault gameObject;

[Tooltip("Event to send if touching the ground.")]
public FsmEvent trueEvent;

[Tooltip("Event to send if not touching the ground.")]
public FsmEvent falseEvent;

[Tooltip("Sore the result in a bool variable.")]
[UIHint(UIHint.Variable)]
public FsmBool storeResult;

[Tooltip("Repeate every frame.")]
public bool everyFrame;

private GameObject previousGo; // remember so we can get new controller only when it changes.
CharacterMotor targetMotor;

public override void Reset()
{
gameObject = null;
trueEvent = null;
falseEvent = null;
storeResult = null;
everyFrame = false;
}

public override void OnEnter()
{
DoMotorIsGrounded();

if (!everyFrame)
{
Finish();
}
}

public override void OnUpdate()
{
DoMotorIsGrounded();
}

void DoMotorIsGrounded()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

if (go != previousGo)
{
targetMotor = go.GetComponent<CharacterMotor>();
previousGo = go;
}

if (targetMotor == null) return;

var onGround = targetMotor.IsGrounded();

storeResult.Value = onGround;

Fsm.Event(onGround ? trueEvent : falseEvent);
}
}
}


Action: Character Motor Velociy. Stores a Vector3 variable that gives the current velocity of the character.
 
This one I posted in the Help section of the forums. Will place here again in case anyone found it useful.
For instance, you can use this variable with Get Vector3 XYZ to extract the Y component and know if your character has reached Jump Apex, etc.

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Character)]
[Tooltip("Obtain Character Motor velocity vector.")]
public class MotorVelocity : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(CharacterMotor))]
[Tooltip("The GameObject to check.")]
public FsmOwnerDefault gameObject;

[Tooltip("Sore the result in a vector3 variable.")]
[UIHint(UIHint.Variable)]
public FsmVector3 storeResult;

[Tooltip("Repeate every frame.")]
public bool everyFrame;

private GameObject previousGo; // remember so we can get new controller only when it changes.
CharacterMotor motor;

public override void Reset()
{
gameObject = null;
storeResult = null;
everyFrame = false;
}

public override void OnEnter()
{
DoMotorVelocity();

if (!everyFrame)
{
Finish();
}
}

public override void OnUpdate()
{
DoMotorVelocity();
}

void DoMotorVelocity()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

if (go != previousGo)
{
motor = go.GetComponent<CharacterMotor>();
previousGo = go;
}

if (motor == null) return;

CharacterMotorMovement movement = motor.movement;
var velocity = movement.velocity;

storeResult.Value = velocity;
}
}
}


Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: Several Character Motor Functions
« Reply #1 on: April 16, 2012, 02:32:14 PM »
These look very useful! Thanks for posting! :D

amaranth

  • Full Member
  • ***
  • Posts: 172
Re: Several Character Motor Functions
« Reply #2 on: May 02, 2012, 04:24:51 PM »
Thank you, Vanlande, this is very helpful! I've made two additional scripts from the ones you have above. They are GetMotorGravity and SetMotorGravity.

GetMotorGravity
Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Character)]
[Tooltip("Obtain Character Motor gravity float.")]
public class GetMotorGravity : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(CharacterMotor))]
[Tooltip("The GameObject to check.")]
public FsmOwnerDefault gameObject;

[Tooltip("Store the result in a float variable.")]
[UIHint(UIHint.Variable)]
public FsmFloat storeResult;

[Tooltip("Repeate every frame.")]
public bool everyFrame;

private GameObject previousGo; // remember so we can get new controller only when it changes.
CharacterMotor motor;

public override void Reset()
{
gameObject = null;
storeResult = null;
everyFrame = false;
}

public override void OnEnter()
{
DoGravity();

if (!everyFrame)
{
Finish();
}
}

public override void OnUpdate()
{
DoGravity();
}

void DoGravity()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

if (go != previousGo)
{
motor = go.GetComponent<CharacterMotor>();
previousGo = go;
}

if (motor == null) return;

CharacterMotorMovement movement = motor.movement;
var gravity = movement.gravity;

storeResult.Value = gravity;
}
}
}

SetMotorGravity
Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Character)]
[Tooltip("Set Character Motor gravity float.")]
public class SetMotorGravity : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(CharacterMotor))]
[Tooltip("The GameObject to check.")]
public FsmOwnerDefault gameObject;

[Tooltip("Assign gravity to the character (variable = float).")]
public FsmFloat gravityValue;

private GameObject previousGo; // remember so we can get new controller only when it changes.
CharacterMotor motor;

public override void Reset()
{
gameObject = null;
gravityValue = null;
}

public override void OnEnter()
{
DoGravity();
Finish();
}

public override void OnUpdate()
{
DoGravity();
}

void DoGravity()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

if (go != previousGo)
{
motor = go.GetComponent<CharacterMotor>();
previousGo = go;
}

if (motor == null) return;

CharacterMotorMovement movement = motor.movement;
movement.gravity = gravityValue.Value;
}
}
}
« Last Edit: May 02, 2012, 06:42:11 PM by amaranth »

Vanlande

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Several Character Motor Functions
« Reply #3 on: May 03, 2012, 10:14:55 PM »
Thanks for sharing mate! I'll be sure to try these out  ;)

Drunkenoodle

  • Playmaker Newbie
  • *
  • Posts: 25
Re: Several Character Motor Functions
« Reply #4 on: August 27, 2012, 03:35:14 PM »
Did I ever tell you how much I love you Vanlande? Thank you for this my friend (just been searching and this post couldn't have been found at a better time). :D