void FixedUpdate()
{
//If the unit have somewhere to go
if (hasTarget) {
Construct construct = GetComponent<Construct>(); //See if the unit has something to cunstruct
//Recalculate path
//if (Time.time - lastRepath > repathRate && seeker.IsDone()) {
// lastRepath = Time.time+ Random.value*repathRate*0.5f;
// seeker.StartPath (transform.position,targetPos, OnPathComplete);
//}
if(targetPosAux != targetPos)
{
seeker.StartPath (transform.position,targetPos, OnPathComplete);
targetPosAux = targetPos;
}
if (path == null) {
return;
}
currentPosition = new Vector2(transform.position.x,transform.position.z);
posTarget = new Vector2(targetPos.x,targetPos.z);
//Distance to the target from the current position
distanceToTarget = Vector2.Distance(currentPosition,posTarget);
//If the unit hasn't reached the target yet
if(!targetReached(distanceToTarget)){
Vector3 dir = (path.vectorPath[currentWaypoint]-transform.position).normalized; //direction to move along
transform.LookAt(transform.position + new Vector3(dir.x,0f,dir.z));
dir *= speed*Time.deltaTime;
characterController.Move(dir);
//If the unit has moved enough go to the next waypoint of the grid
if ( (transform.position-path.vectorPath[currentWaypoint]).sqrMagnitude < nextWaypointDistance*nextWaypointDistance && currentWaypoint < currentPathCount - 1) currentWaypoint++;
} else {
if(status != Status.attacking)
{
if (construct == null)
stopUnit();
if (construct != null && !construct.getConstruct())
stopUnit();
}
}
}
}