Hi, I have a simple sandbox created with a player and a few cubs patrolling through a set of waypoints that I have stored in a list. It is working fine using the following code to give patrols their target destination…
transform.position = Vector3.MoveTowards (transform.position, currentWaypoint.position, speed * Time.deltaTime);
I have then decided to add a nav mesh so as to include some obstacles for the patrols. To do this, I added a nav agent to the patrol cubes and then use the following code to set their target destination…
navAgent.SetDestination(currentWaypoint.position);
I find when I use this navAgent.SetDestination that the cube reaches the first waypoint and then tips over and gets stuck. I have tried to freeze rotation in the inspector but that seems to make no difference.
Here is the code for the function I have created.
public void moveEnemy()
{
if (GameObject.Find ("Player") != null && PT.isDetected == true) {
Debug.Log ("The Player is in detected!");
currentPatrolTarget = GameObject.Find ("Player").transform;
navAgent.SetDestination(currentPatrolTarget.position);
//transform.position = Vector3.MoveTowards (transform.position, currentPatrolTarget.transform.position, speed * Time.deltaTime);
} else
{
float distanceToCurrent = Vector2.Distance (transform.position, currentWaypoint.position);
if (distanceToCurrent == 0) {
if (wayPointNumber != wayPointsList.Count - 1) {
wayPointNumber++;
currentWaypoint = wayPointsList [wayPointNumber];
} else {
wayPointNumber = 0;
currentWaypoint = wayPointsList [wayPointNumber];
}
}
navAgent.SetDestination(currentWaypoint.position);
//transform.position = Vector3.MoveTowards (transform.position, currentWaypoint.position, speed * Time.deltaTime);
//Establish the transform to give the raycast a target to rotate too
Vector3 relativePos = currentWaypoint.position - transform.position;
transform.rotation = Quaternion.LookRotation (relativePos);
// targetRotation would be the result of your Quat.LookRotation call
// transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
Any thoughts or advice appreciated.
2h