playMaker

Author Topic: Pathfinding Set Agent Destination Waypoints  (Read 6402 times)

FractalCore

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 100
Pathfinding Set Agent Destination Waypoints
« on: March 23, 2012, 06:02:43 AM »
I'm trying to set up some waypoints for an AI character. I can get it to the first one, but I'm having trouble telling the character it's reached the destination, and should now head for the next.

I'm using Set Agent Destination and I thought the Path Pending Done Event might be the way to trigger it, but I realised it's for triggering something else, before the character sets off.

Is the Agent Ray Cast action a way to do it, or am I trying to go about it the wrong way.

Whatever it takes to make a character walk around autonomously.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Pathfinding Set Agent Destination Waypoints
« Reply #1 on: March 23, 2012, 08:12:33 AM »
Hi,

 This is exactly where pathfinding frameworks fails to deliver a suitable api... so...

 The way to go about it would be set up a trigger around the destination, so that when an agent trigger that volume, you know he's close enough and you can then set him to the next destination.

 that would be the more sensible and 100% reliable system. Now it could be problematic if several agents are trying to access the same way point, but that would be already something to adress specifically for your needs.

I am not sure I can propose another solution that would be as reliable.

 Don't hesitate to report back this via the unity bug reporting, I did, the more they hear about this, the better, they might address it properly in a future update.

 bye,

 Jean

FractalCore

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 100
Re: Pathfinding Set Agent Destination Waypoints
« Reply #2 on: March 26, 2012, 06:08:38 AM »
I found the Get Agent Remaining Distance action. It didn't work at first. It would trigger instantly when I had the Get Distance as the start state, so as you can see in the picture, I put a wait in first (wait for 0.1), now it works. This might be a bug with this Action. It was passing straight through as if the Agent had arrived.

I also got the collision method to work. But I had to add a Character Controller Component (which does nothing here) to go with the Nav Mesh Agent Component, or else nothing was triggering collisions. So each waypoint has a box collider set to Is Trigger, then a state that triggers with a Trigger Enter transition event. Couldn't get anything else to work. Not sure if there's a better way.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Pathfinding Set Agent Destination Waypoints
« Reply #3 on: March 27, 2012, 01:51:29 AM »
uhm...

I think it might be due to the fact that the agent doesn't have yet a path assigned and therefore the remaining distance doesn't make sense until it has computed its path.

try to check first with GetAgentPathPending action or GetAgentPathStatus action. These two actions will inform you about the state of the path for that agent, and then decide if you check for the remaining distance or not.

 Bye,

 Jean

AnyaNam

  • Playmaker Newbie
  • *
  • Posts: 2
Re: Pathfinding Set Agent Destination Waypoints
« Reply #4 on: July 30, 2016, 08:46:39 AM »
Hi,

I'm having the same problem, could you upload a picture on how you got this to work? :)

tchrisbaker

  • Playmaker Newbie
  • *
  • Posts: 8
Re: Pathfinding Set Agent Destination Waypoints
« Reply #5 on: August 09, 2016, 07:17:05 PM »
I altered the Set Agent Destination As Game Object action to use a Finished action. I think the regular Set Agent Destination should be done this way too.

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{

[ActionCategory(ActionCategory.NavMeshAgent)]
[Tooltip("Set the destination of a NavMesh Agent to a gameObject. \nNOTE: The Game Object must have a NavMeshAgentcomponent attached.")]
public class SetAgentDestinationAsGameObjectWithEvent : FsmStateAction
{
[RequiredField]
[Tooltip("The Game Object to work with. NOTE: The Game Object must have a NavMeshAgent component attached.")]
[CheckForComponent(typeof(NavMeshAgent))]
public FsmOwnerDefault gameObject;

[RequiredField]
[Tooltip("Set the destination to navigate towards.")]
public FsmGameObject destination;

[RequiredField]
[Tooltip("Define the gameObject minimum movement distance to retrigger path computation.")]
public FsmFloat triggerThreshold;

[Tooltip("Distance from target to be considred in range.")]
public FsmFloat withinRange;

[Tooltip("Define the event called when in range of target.")]
public FsmEvent eventWhenInRange;

private NavMeshAgent _agent;

private Vector3 lastPosition;

private float lastDistance;

private float squareDistance;

GameObject _lastGo;

private void _getAgent()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null)
{
return;
}

_agent =  go.GetComponent<NavMeshAgent>();
}

public override void Reset()
{
gameObject = null;
destination = null;
triggerThreshold = 1;
eventWhenInRange = null;
withinRange = 0;

}

public override void OnEnter()
{
// force refresh
_lastGo = null;

_getAgent();

DoSetDestination();

}

public override void OnUpdate()
{
GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);

if (Vector3.Distance (destination.Value.transform.position, go.transform.position) <= withinRange.Value + 1 ) {
Fsm.Event(eventWhenInRange);
}

else
DoSetDestination();
}

void DoSetDestination()
{

if (destination.Value == null || _agent == null)
{
return;
}

bool forceUpdate = false;
if (_lastGo != destination.Value)
{
forceUpdate = true;
_lastGo = destination.Value;
}

if (lastDistance!= triggerThreshold.Value){
lastDistance = triggerThreshold.Value;
squareDistance = lastDistance*lastDistance;
}

Vector3 newPosition = destination.Value.transform.position;
Vector3 deltaPosition = newPosition-lastPosition;

if (deltaPosition.sqrMagnitude>squareDistance || forceUpdate)
{
_agent.SetDestination(newPosition);
_agent.stoppingDistance = withinRange.Value - 1;
lastPosition = destination.Value.transform.position;
}

}


}
}

kavery

  • Full Member
  • ***
  • Posts: 149
Re: Pathfinding Set Agent Destination Waypoints
« Reply #6 on: August 10, 2016, 09:50:27 AM »
This might not be useful for your setup, but I do something similar with 'move towards' .. it has a reliable way to determine a reached destination. The adjustable 'finish distance' is nice for tweaking the specific end point.