PlayMaker Updates & Downloads > Share New Actions

Get Velocity Float (no Rigidbody required)

(1/1)

Geropellicer:
Hi, I modified an action originally intended to get the Characters Controller velocity, so now it gets the velocity of a gameobject independently if it has a rigidbody or not and indepenently if it has a character controller.
The velocity (or speed, I don't get the difference quite well) is the absolute speed the gameobject is moving relative to the world, returned as a float, NOT as a Vector 3.


--- Code: ---using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Character)]
[Tooltip("Gets the overall velocity of a character expressed in a float")]
public class GetVelocityFloat : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmFloat storeResult;
public bool everyFrame;

GameObject previousGo;


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

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

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


Vector3 posVieja = new Vector3(0,0,0);
public void doGetVelocity()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null) return;

if (go != previousGo)
{
previousGo = go;
}


Vector3 posActual = go.transform.position;


Vector3 horizontalVelocity = (posActual - posVieja) / Time.deltaTime;


float horizontalSpeed = horizontalVelocity.magnitude;
storeResult.Value = horizontalSpeed;

posVieja = go.transform.position;
}
}
}

--- End code ---

jeanfabre:
Hi,

 Thanks for your contribution. Have you checked also GetGameObjectSpeed on the ecosystem? I think it's similar.

https://github.com/jeanfabre/PlayMakerCustomActions_U4/blob/master/Assets/PlayMaker%20Custom%20Actions/GameObject/GetGameObjectSpeed.cs

 Bye,

 Jean

Navigation

[0] Message Index

Go to full version