I have RVO set up on two agents and the one’s behind the one near the target kind of just stop in place. Perhaps its because they chose the same path? Is there a way to work around this without having to code a system to make them find alternate paths?
Hi
Which movement script are you using?
using UnityEngine;
// Note this line, if it is left out, the script won't know that the class 'Path' exists and it will throw compiler errors
// This line should always be present at the top of scripts which use pathfinding
using Pathfinding;
using System.Collections;
using Pathfinding.RVO;
[RequireComponent(typeof(RichAI), typeof(RVOController))]
public class PathfindingHandler : MonoBehaviour
{
private Seeker _seeker;
[SerializeField] private Path _path;
[SerializeField] private Vector3[] _pathCorners;
[Header("Parameters")]
[SerializeField] private float _speed = 2;
[SerializeField] private float _nextWaypointDistance = .5f;
[Header("Trackers")]
[SerializeField] private int _currentWaypoint = 0;
[SerializeField] private bool _reachedEndOfPath;
public Vector3 PathDirection => _path != null && !_reachedEndOfPath ? _path.vectorPath[_currentWaypoint] : Vector3.zero;
public void Awake()
{
_seeker = GetComponent<Seeker>();
}
public void SetTargetPosition(Vector3 pos)
{
_seeker.StartPath(transform.position, pos, OnPathComplete);
}
public void OnPathComplete(Path p)
{
//Debug.Log("A _path was calculated. Did it fail with an error? " + p.error);
if (!p.error)
{
_path = p;
_pathCorners = _path.vectorPath.ToArray();
// Reset the waypoint counter so that we start to move towards the first point in the _path
_currentWaypoint = 0;
}
}
public void UpdatePathProgress()
{
if (_path == null)
{
// We have no _path to follow yet, so don't do anything
return;
}
// Check in a loop if we are close enough to the current waypoint to switch to the next one.
// We do this in a loop because many waypoints might be close to each other and we may reach
// several of them in the same frame.
_reachedEndOfPath = false;
// The distance to the next waypoint in the _path
float distanceToWaypoint;
while (true)
{
// If you want maximum performance you can check the squared distance instead to get rid of a
// square root calculation. But that is outside the scope of this tutorial.
distanceToWaypoint = Vector3.Distance(transform.position, _path.vectorPath[_currentWaypoint]);
if (distanceToWaypoint < _nextWaypointDistance)
{
// Check if there is another waypoint or if we have reached the end of the _path
if (_currentWaypoint + 1 < _path.vectorPath.Count)
{
_currentWaypoint++;
}
else
{
// Set a status variable to indicate that the agent has reached the end of the _path.
// You can use this to trigger some special code if your game requires that.
_reachedEndOfPath = true;
break;
}
}
else
{
break;
}
}
// Slow down smoothly upon approaching the end of the _path
// This value will smoothly go from 1 to 0 as the agent approaches the last waypoint in the _path.
var speedFactor = _reachedEndOfPath ? Mathf.Sqrt(distanceToWaypoint / _nextWaypointDistance) : 1f;
Vector3 dir = (_path.vectorPath[_currentWaypoint] - transform.position).normalized;
// Multiply the direction by our desired _speed to get a velocity
Vector3 velocity = dir * _speed * speedFactor;
}
private void OnDrawGizmosSelected()
{
if (_path == null)
{
return;
}
foreach (var corner in _pathCorners)
{
Gizmos.DrawWireSphere(corner, .2f);
}
}
}
It’s pretty much a clone of the one found on the documentation from here Movement scripts - A* Pathfinding Project
I have the variable PathDirection read by the main script which controls the movement. Disregard the RichAI part, I was using AIPath component but wanted to see if switching the pathfinding type would fix the issue.