Search Unity

Moviment and Spawn of AI

Discussion in 'Navigation' started by vtcarvalhal, Jun 2, 2021.

  1. vtcarvalhal

    vtcarvalhal

    Joined:
    Jun 2, 2021
    Posts:
    1
    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):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Patrol : MonoBehaviour
    6. {
    7.     public float speed;
    8.     private float waitTime;
    9.     public float startWaitTime;
    10.  
    11.     public Transform[] moveSpot1;
    12.     public Transform[] moveSpot2;
    13.     private int randonSpot;
    14.  
    15.     public Transform[] spawnSpot;
    16.     private int randonSpawn;
    17.  
    18.    
    19.     void Start()
    20.     {
    21.        
    22.         randonSpawn = Random.Range(0, spawnSpot.Length);
    23.        
    24.         Debug.Log(randonSpawn);
    25.        
    26.         transform.position = spawnSpot[randonSpawn].position;
    27.        
    28.         waitTime = startWaitTime;
    29.        
    30.         if (randonSpawn == 0)
    31.         {
    32.            
    33.             randonSpot = Random.Range(0, moveSpot1.Length);
    34.         }
    35.         else
    36.         {
    37.            
    38.             if (randonSpawn == 1)
    39.             {
    40.                
    41.                 randonSpot = Random.Range(0, moveSpot2.Length);
    42.             }
    43.         }
    44.        
    45.     }
    46.  
    47.     void Update()
    48.     {
    49.        
    50.         if (randonSpawn == 0){
    51.            
    52.             transform.position = Vector3.MoveTowards(transform.position, moveSpot1[randonSpot].position, speed * Time.deltaTime);
    53.          
    54.             if (Vector3.Distance(transform.position, moveSpot1[randonSpot].position) < 0.2f) {
    55.                
    56.                 if (waitTime <= 0) {
    57.                    
    58.                     randonSpot = Random.Range(0, moveSpot1.Length);
    59.                    
    60.                     waitTime = startWaitTime;
    61.                 }
    62.                 else {
    63.                    
    64.                     waitTime -= Time.deltaTime;
    65.                 }
    66.             }
    67.         }
    68.         else
    69.         {
    70.             if (randonSpawn == 1)
    71.             {
    72.                 transform.position = Vector3.MoveTowards(transform.position, moveSpot2[randonSpot].position, speed * Time.deltaTime);
    73.                 if (Vector3.Distance(transform.position, moveSpot2[randonSpot].position) < 0.2f)
    74.                 {
    75.                     if (waitTime <= 0)
    76.                     {
    77.                         randonSpot = Random.Range(0, moveSpot2.Length);
    78.                         waitTime = startWaitTime;
    79.                     }
    80.                     else
    81.                     {
    82.                         waitTime -= Time.deltaTime;
    83.                     }
    84.                 }
    85.             }
    86.         }
    87.     }
    88. }
     
  2. ChrisKurhan

    ChrisKurhan

    Joined:
    Dec 28, 2015
    Posts:
    268
    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.