I wanna make an AI who at the start of the game chooses randomly a spawn point in the map to spawn. And then depending on the spawn point it is gonna movement to a certain point in the map around this spawn point. Ex: Started the game, Ai chosed to spawn on spawn_point_1. Now it can only moviment to movement_point_1.1, movement_point_1.2, movement_point_1.3. I made a code that does that. But it has some problems: 1 - The code only functions if I create a vector beforehand for the movements_points that a spawn point can access. So if I wanna use a lot of spawn points I have to go manually to the script create a new group of moviments_points. I wanna make It less time-consuming, but I don't know what to do. 2 - The ai is placed in a map that has elevations and objects, that it should notice and collide with. But, every time that I put a rigid body on the object nothing happens because I'm using movement points and spawn points. I tried to put gravity in the points, but it didn't work. The AI started grabbing the points and moving them all over the map. Now I don't know how to resolve these bugs. Can someone help me? Here the code that I used: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Patrol : MonoBehaviour { public float speed; private float waitTime; public float startWaitTime; public Transform[] moveSpot1; public Transform[] moveSpot2; private int randonSpot; public Transform[] spawnSpot; private int randonSpawn; void Start() { randonSpawn = Random.Range(0, spawnSpot.Length); Debug.Log(randonSpawn); transform.position = spawnSpot[randonSpawn].position; waitTime = startWaitTime; if (randonSpawn == 0) { randonSpot = Random.Range(0, moveSpot1.Length); } else { if (randonSpawn == 1) { randonSpot = Random.Range(0, moveSpot2.Length); } } } void Update() { if (randonSpawn == 0){ transform.position = Vector3.MoveTowards(transform.position, moveSpot1[randonSpot].position, speed * Time.deltaTime); if (Vector3.Distance(transform.position, moveSpot1[randonSpot].position) < 0.2f) { if (waitTime <= 0) { randonSpot = Random.Range(0, moveSpot1.Length); waitTime = startWaitTime; } else { waitTime -= Time.deltaTime; } } } else { if (randonSpawn == 1) { transform.position = Vector3.MoveTowards(transform.position, moveSpot2[randonSpot].position, speed * Time.deltaTime); if (Vector3.Distance(transform.position, moveSpot2[randonSpot].position) < 0.2f) { if (waitTime <= 0) { randonSpot = Random.Range(0, moveSpot2.Length); waitTime = startWaitTime; } else { waitTime -= Time.deltaTime; } } } } } }
In the thread NavMesh Tutorial Resources I have linked a video that shows you how to spawn enemies at random points on a NavMesh, that's in AI Series Part 4. In the AI Series Part 11 I went over how to set up a state machine including picking random points on a NavMesh to patrol as well as random idle movement. Between those two I think they will help you with what you are trying to implement here.