I found getting the property from NavMesh was not always working well (the drag and drop method worked fine, but not using referencing it through object variable), so I made this simple action.
Strange School's
example was useful in making it (check out the channel as linked there), I'm myself not a coder.
// Fetches Nav Mesh Velocity directly.
// NOTE: you need to set up a Vector3 variable in
// Playmaker and set this one in "Store Velocity" and take it from there!
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.NavMeshAgent)]
public class GetNavMeshVelocity : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(UnityEngine.AI.NavMeshAgent))]
[Tooltip("holder of the NavMeshAgent script")]
public FsmOwnerDefault gameObject;
// variables we need.
[Tooltip("holder of the target FSM")]
public FsmGameObject targetGameobject;
public FsmVector3 storeVelocity;
// Enable or disable Every Frame
public FsmBool everyFrame;
// Left is the script type, right the variable where it will be stored for use.
UnityEngine.AI.NavMeshAgent scriptObject;
//On Enter
public override void OnEnter()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
// Confusing as it is, but simple. We declared above the scriptObject type we want, but it is still empty.
// Hence, we now need to access the object (thats the "var go" just above),
// and fetch from there the concrete object of the same type.
scriptObject = go.GetComponent<UnityEngine.AI.NavMeshAgent>();
if (!everyFrame.Value)
{
GetVelocity();
Finish();
}
}
//Public Override
public override void OnUpdate()
{
if (everyFrame.Value)
{
GetVelocity();
}
}
// Ze Method
void GetVelocity()
{
var go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}
// Executing the actual operation. FSM velocity is set to that from the scriptObject.
storeVelocity.Value = scriptObject.velocity;
}
}
}
Comments or feedback appreciated, thanks!