playMaker

Author Topic: getGameObjectSpeed ( no physics required)  (Read 4266 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14342
  • Official Playmaker Support
    • View Profile
getGameObjectSpeed ( no physics required)
« on: March 07, 2012, 01:06:36 AM »
Hi,

 Following a request, here's n action that get the speed of any gameObject, it doesn't require a physics component attached.

[Edit] this script is now available on the Ecosystem.

Code: [Select]
// (c) copyright Hutong Games, LLC 2010-2012. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.GameObject)]
[Tooltip("Get the speed of a gameObject. No need to have it set up with a physic component.")]
public class GetGameObjectSpeed : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;

[Tooltip("The current speed")]
[UIHint(UIHint.Variable)]
public FsmFloat speed;

[Tooltip("The current speed vector")]
[UIHint(UIHint.Variable)]
public FsmVector3 speedVector;

private GameObject go;

private Vector3 lastPosition;

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

public override void OnEnter()
{
go = Fsm.GetOwnerDefaultTarget(gameObject);
lastPosition = go.transform.position;

}

public override void OnUpdate()
{
if (go == null)
{
return;
}

doComputeSpeed();
}


void doComputeSpeed()
{
Vector3 currentPosition = go.transform.position;

Vector3 delta = currentPosition-lastPosition;
if (!speed.IsNone){
speed.Value = delta.magnitude/Time.deltaTime;
}
speedVector.Value = delta;

lastPosition = currentPosition;
}

}
}


Bye,

 Jean
« Last Edit: March 25, 2016, 08:27:05 AM by jeanfabre »

justifun

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 228
    • View Profile
Re: getGameObjectSpeed ( no physics required)
« Reply #1 on: March 07, 2012, 10:07:02 AM »
Thank you!!!!!

Dev_Sebas

  • 1.2 Beta
  • Sr. Member
  • *
  • Posts: 398
    • View Profile
    • Blog
Re: getGameObjectSpeed ( no physics required)
« Reply #2 on: September 10, 2012, 02:22:37 AM »
ThankĀ“s Jean. :)
Bye

koenlaan

  • Playmaker Newbie
  • *
  • Posts: 1
    • View Profile
Re: getGameObjectSpeed ( no physics required)
« Reply #3 on: March 25, 2016, 07:55:51 AM »
Dear Jean

Thanks for your script.
This really what i want and need!
I am quite new to scripting and playmaker
how do i get it to work?
I cant just dragg the script on a object.
 I must do something wrong
Could you explain a bit?
thanks!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14342
  • Official Playmaker Support
    • View Profile
Re: getGameObjectSpeed ( no physics required)
« Reply #4 on: March 25, 2016, 08:26:07 AM »
Hi,

 Welcome to PlayMaker! :)

This script is a special script, it's not something you drag directly onto the GameObject, but rather drag inside your Project folder, and PlayMaker will recognized it as an Action you can use like any other, and so once you have this script inside your project folder, it will be listed in the Action Browser, and you can drag it from the Action browser into a Fsm State.

 Does that make more sense?

 Also, I strongly recommend you use the Ecosystem browser, it's meant to solve the issue you are having. This script is actually registered within a system that let you search for custom actions, samples, and packages for PlayMaker, and in one click it gets downloaded and installed for you, ready to be used within your project.




Let me know if you still struggle with this.

Bye,

 Jean