playMaker

Author Topic: How to detect speed or movement of a Character  (Read 5062 times)

Sly

  • Full Member
  • ***
  • Posts: 123
How to detect speed or movement of a Character
« on: September 17, 2014, 02:07:10 PM »
Hello,

I want to know if it's possible to detect the speed or movement of a character.
I want to know if my character is moving or not. I'm trying to use action "Get Speed" or "Get Velocity".
But the result is not stopping. Instead of showing a static value (the actual speed), the value is increased again and again. So I can't really see the speed or velocity of my character.
Can someone tells me what I'm doing wrong, because I don't understand what's going on.
Thanks

PS: I'm using the Aron Astar Pathfinding; but it's using the basic rigid body

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to detect speed or movement of a Character
« Reply #1 on: September 18, 2014, 09:49:02 AM »
Hi,

 getspeed should work ( must work), I guess you are not checking the value using a threshold, use "float compare" and give it some room so that the speed around 0 is considered as not moving.

 Bye,

 Jean

Sly

  • Full Member
  • ***
  • Posts: 123
Re: How to detect speed or movement of a Character
« Reply #2 on: September 19, 2014, 04:25:37 PM »
Ok, I understand why it's not working. The speed is manage by the pathfinding system so I add Rigidbody with freeze coordinates, and I did it in purpose in my project.
So I succeed with Velocity: I check if X and Z. And with a conditional action I check if they have a value of 0.

It could be cool to have an action to check if a game object is static by checking speed or velocity (in my case) with a choosing delay.
If my velocity or speed is 0 for 3 second so launch an event.

Do you think it could be a good action request?
 

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to detect speed or movement of a Character
« Reply #3 on: October 15, 2014, 01:34:16 AM »
Hi,

 you can do this very simply with existing actions, get the speed, and the next action is a compare float, that fires an event if above a certain threshold.

Have you tried this already? I do that many times, and never felt the need to create a custom action for this actually, because it's so specific to a property that it's "better" to simply work on a generic float, so one action to get the float from the property, and then use some comparision tools on that float.


 Bye,

 Jean

RaHaN

  • Playmaker Newbie
  • *
  • Posts: 5
Re: How to detect speed or movement of a Character
« Reply #4 on: August 26, 2016, 09:14:21 AM »
Hi,

I just updated your action to include separate X, Y, Z coordinates of the velocity vector, pasting it here if anyone wants to update this action:

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Character)]
[Tooltip("Gets the velocity of the attached character controller")]
public class GetControllerVelocity : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(CharacterController))]
public FsmOwnerDefault gameObject;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmVector3 storeResult;
[UIHint(UIHint.Variable)]
public FsmFloat x;
[UIHint(UIHint.Variable)]
public FsmFloat y;
[UIHint(UIHint.Variable)]
public FsmFloat z;
public bool everyFrame;

GameObject previousGo;
CharacterController controller;

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

public override void OnEnter()
{
doGetVelocity();
if (!everyFrame) Finish();
}

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

public void doGetVelocity()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null) return;

if (go != previousGo)
{
controller = go.GetComponent<CharacterController>();
previousGo = go;
}

if (controller != null)
{
storeResult.Value = controller.velocity;
x.Value = controller.velocity.x;
y.Value = controller.velocity.y;
z.Value = controller.velocity.z;
}
}
}
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: How to detect speed or movement of a Character
« Reply #5 on: September 05, 2016, 04:31:38 AM »
Hi,

 Excellent thanks  for your contribution!

If you feel like using the ecosystem for sharing this, let me know, there are some videos explaning how you can distribute your actions on the Ecosystem for others to easily download your actions.


 Bye,

 Jean