Need tips on agent movement

Ok so i got a little problem. I am changing my units movement.
alt text

Before the player could only click around in the circle to move around. That worked well. But now i am trying to change it all so the player can click outside the circle and the unit will move til it is not outside the area. That worked well for a first run. But it is then stuck.

	void IAmMoving()
	{

		float distFromCent = Vector3.Distance(_currentActiveUnit._agent.nextPosition,_currentActiveUnit._fieldPosition);
		float allowedRange = _field.transform.localScale.x; 	
		
		if(distFromCent>allowedRange)
		{
			
			
			_currentActiveUnit._agent.Stop();
			_moving = false;
			Debug.Log("Stopping");
		}	
	}

I am trying to check the navmesh agents nextPosition. But that position is outside the circle at all time when it is outside once. Even if i turn the unit so it is facing the new area. So do you guys have any good tips on how i should fix this problem

1 Answer

1

I fixed the problem by doing the following

	void DoWhileMoving() //IAmMoving function
	{
		Vector3 ahead = _currentActiveUnit.transform.position + _currentActiveUnit.transform.forward * 1f;
		float distFromCent = Vector3.Distance(ahead,_currentActiveUnit._fieldPosition);
		float allowedRange = _field.transform.localScale.x * 1f; 	
		
		if(distFromCent>allowedRange)
		{
			_currentActiveUnit._agent.Stop();
			_moving = false;
			//_GUIchange.SetButtonActive(true);
			Debug.Log("Stopping");
		}		
	}

Ok so the only thing i did instead was to check one unit infront of the moving unit. So if it is outside the area. The Unit will stop