I'm trying to make an Enemy AI. I decided to start with random patrolling before making the attack and other. I tried this: Code (CSharp): public NavMeshAgent agent; public Vector3 RandomPoint; public Vector3 RandomPointOnTerrain; public bool isPatrolling; void Start(){ agent = GetComponent<NavMeshAgent>(); } void Update(){ if(isPatrolling == false){ //it finished the current patrolle and need other point RandomPoint = Random.insideUnitSphere * 6; //find point in sphere RandomPointOnTerrain = new Vector3(RandomPoint.x, Terrain.activeTerrain.SampleHeight(RandomPoint), RandomPoint.z); //find the random point on terrain agent.SetDestination(RandomPointOnTerrain); //set the patrolling target isPatrolling = true //it received a target } if(agent.remainingDistance > 0){ isPatrolling = true; //still patrolling } else{ isPatrolling = false; //it finished the current patrolle and need other point } } It doesn't work.
A screenshot would probably help. Also what exactly doesn't work. Looking at your code agent.remainingDistance > 0 doesn't work. You have to subtract your agent.StoppingDistance, otherwise you will probably never reach 0 on the remaining distance.
Ok, thank you for this information. Only this is a problem with the code? Because the enemies doesn't move.
Could you show a screenshot of you generated NavMesh? Did you try to debug your generated position? If you want to make sure your generated position are valid, use NavMesh.SamplePosition.
@MikeyJY I would suggest you test everything one by one, so that you see which thing works and which doesn't. That way you can isolate your problem faster. i.e. visualize/do Debug.Log prints for those random points, check that they are valid like suggested above and so on.
@DwinTeimlon The navmesh works for other AI animals @Olmi I'm doing this. In all my script I have a public bool isScriptHere. I enable after a line of code that I'm not sure it is executing and I i see the checkbox checked in Inspector that means the line is executed.