Rapid path recalculation advice

Hi Aron,

I have a game where theres going to be lots of units (lets say 20+) and they will need to have dynamic targets. They will be constantly following objects which have changing positions.

Now I currently have a timer of about 0.3f that I check if the targets position has moved and then I re-calc the path. However its noticably ‘laggy’ the way units update their path needs.

If I reduced time to re-calc a path below 0.3f I ran into issues with the path creation.

Can you advise how I can handle a situation where I need constant path updates from many objects?

Hi

Could you elaborate?

However its noticably 'laggy' the way units update their path needs.
Laggy as in low fps or jittery movement.

What movement script are you using?

If I reduced time to re-calc a path below 0.3f I ran into issues with the path creation.
What problems specifically?

Sure here is the sample code of when I recall the pathfinding

//This is in the FixedUpdate()

`if(Vector3.Distance(transform.position,_dataCenter.enemyTarget.transform.position) > _attackRange)
			{
				enemyPositionToMoveTo = _dataCenter.enemyTarget.transform.position;
			}

Vector3 clampedPathPosition = new Vector3(_path.vectorPath[_currentWaypoint].x,0,_path.vectorPath[_currentWaypoint].z);
		Vector3 clampedCurrentPosition = new Vector3(transform.position.x,0,transform.position.z);
		Vector3 direction = (clampedPathPosition - clampedCurrentPosition).normalized;
		direction = new Vector3(direction.x,0,direction.z).normalized;
		Vector3 distance = direction*_speed*Time.fixedDeltaTime;
		Quaternion finalRotation = Quaternion.LookRotation(clampedPathPosition - clampedCurrentPosition);
		transform.rotation = Quaternion.Lerp(transform.rotation,finalRotation,_rotationSpeed*Time.deltaTime);
		transform.position += distance;
		
		Vector3 clampedGameObjectPostion = new Vector3(transform.position.x,0,transform.position.z);
		if(Vector3.Distance(transform.position,clampedPathPosition) < _waypointDistance)
		{
			_currentWaypoint++;
		}

`

If I dont have a timer of 0.3f, the unit will constantly stutter and move in a jittered manner.

Hi

I think you should put the distance check for the next waypoint at the top of the function, and also make it a while loop.
Otherwise what might happen is that the path is calculated, but the first waypoint is already “reached”, but since the movement code is before the distance check code, it will move towards it anyway for one frame and cause jittery movement.

Also, you could try the included movement scripts or just take a look at how they move the character.