Hello there , I have a guarding script in which enemy patrol around specific area and I have provided waypoints to the gameobject but the problem is that it movies like the game is lagging . Its movement is not smooth . I have made a coroutine and did "Startcoroutine" in START() function .
Following is my code >
IEnumerator FollowWayPoint(Vector3[] waypoints)
> {> //set the enemy position to first wayPoint> transform.position = waypoints[0];
int targetPointIndex = 1;
Vector3 targetPoint = waypoints[targetPointIndex];
while (true)
{
// Make the enemy move towards the target Point.
transform.position = Vector3.MoveTowards(transform.position, targetPoint, speed * Time.deltaTime);
// When Enemy is on the target Point .
if(transform.position == targetPoint)
{
// Increase the targetPointIndex by 1.
targetPointIndex = (targetPointIndex + 1) % waypoints.Length;
}
targetPoint = waypoints[targetPointIndex];
yield return new WaitForSeconds(waitTime);
}
}
↧