playMaker

Author Topic: Get NavMesh Velocity  (Read 3133 times)

Thore

  • Sr. Member
  • ****
  • Posts: 480
Get NavMesh Velocity
« on: March 28, 2017, 12:33:47 PM »
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.

Quote
// 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!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Get NavMesh Velocity
« Reply #1 on: April 03, 2017, 06:46:30 AM »
Hi,

 I think you should instead use the dedicated addon package on the Ecosystem, it has pretty much all the custom actions for working with pathfingiind, including getting the velocity of an agent (GetAgentVelocity)



Bye,

 Jean

Thore

  • Sr. Member
  • ****
  • Posts: 480
Re: Get NavMesh Velocity
« Reply #2 on: April 05, 2017, 04:44:29 PM »
Superb! Searching for pathfinding didn't occur to me (I never used NavMesh before). Thanks, Jean.