playMaker

Author Topic: [SOLVED] Get jump apex using Character Motor  (Read 2951 times)

Vanlande

  • Playmaker Newbie
  • *
  • Posts: 5
[SOLVED] Get jump apex using Character Motor
« on: April 12, 2012, 01:11:24 PM »
Howdy guys. I'm running into a few problems while building a sorta complex animation system that transitions from several states, like Jumping, Falling, Landing, etc. So far I've been able to tap into the Character Motor code and make my own custom Actions that tell me when the character is grounded (a copy pasta of the Controller Is Grounded action :P) or if he's jumping.

What I'm looking to do now is figure out a way to know when the character motor has reached jump apex so I can transition to the "Falling" animation, since the jumping variable only becomes false when the motor touches the ground. I'm thinking it could be done with the character' Y speed, but I'm just not sure on how to do it, or even if that's the best approach. Also, if it can be achieved using the character at all? Any help would be appreciated. Thanks.

Ed.
« Last Edit: April 14, 2012, 05:28:26 PM by Alex Chouls »

justifun

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 280
Re: Get jump apex using Character Motor
« Reply #1 on: April 12, 2012, 03:45:12 PM »
There's a get object speed action
http://hutonggames.com/playmakerforum/index.php?topic=1182.0

but it will probably only be helpful for telling how fast an object is moving overall.

What you could do is put a check via a float compare into your jumping section of your FSM to check the Y value to the previous Y value and if its negative, then start the falling animation.

eg: after pressing jump, you transition to your state that plays your jump animation then to the next state
that has a "get position" action and a float compare

Get Position Action: Store Y value as Yspeed variable
Check Enable: Every Frame

Float Compare Action:

Float 1: Yspeed
Float 2: Yspeed

Less Than: GoToFallingState

So each frame it checks your player's Y position if its less than its previous value and if so, transitions to your falling animation state.




Vanlande

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Get jump apex using Character Motor
« Reply #2 on: April 12, 2012, 10:34:45 PM »
Thanks mate! I actually managed to solve the problem by making a "copy pasta" action of Get Component, and turning it into CharacterMotorVelocity, where I get the CharacterMotorMovement.velocity vector3, and store it in a variable. Then I use a standard Get Vector3 XYZ to take the Y variable. That variable is positive when the character is going up, and turns negative when he's falling, therefore I just use that to do a float compare.

Here's the code, in case someone needs it. I'm pretty sure anyone could have do it themselves, since its mostly a hack job  :D
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;
}
}
}