Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Spawning navmesh agents without floating

Discussion in 'Scripting' started by PhantomProgramming, Apr 1, 2019.

  1. PhantomProgramming

    PhantomProgramming

    Joined:
    Jan 16, 2019
    Posts:
    60
    Hey there, I'm trying to spawn navmesh agents in a circle on a navmesh, however, the way I have my code set up, some spawn floating above the navmesh and don't move. How would I avoid this?
    Spawn code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class AISpawn : MonoBehaviour
    6. {
    7.  
    8.     public GameObject objectToSpawn;
    9.     public GameObject wave2;
    10.     public GameObject wave3;
    11.     public int numEnemies = 0;
    12.     public int numFriendlies = 0;
    13.  
    14.  
    15.     public float spawnRadius = 12f;
    16.     private Vector3 spawnPos;
    17.     public ClassButton cb;
    18.     public bool hasSpawned = false;
    19.     public bool friendly;
    20.    
    21.  
    22.     public bool scienario = false;
    23.  
    24.     public float waveTimer;
    25.  
    26.     // Start is called before the first frame update
    27.     void Start()
    28.     {
    29.         cb = GameObject.FindObjectOfType<ClassButton>();
    30.         numEnemies = cb.integer_Value_we_Want;
    31.         numFriendlies = cb.integer_Value_we_Want2;
    32.  
    33.      
    34.         // numEnemies = cb.lastValue;
    35.     }
    36.     public void FixedUpdate()
    37.     {
    38.        
    39.     }
    40.     // Update is called once per frame
    41.     void Update()
    42.     {
    43.  
    44.         waveTimer += 0.1f;
    45.         if(waveTimer > 300)
    46.         {
    47.             wave2.SetActive(true);
    48.         }
    49.         if (waveTimer > 500)
    50.         {
    51.             wave3.SetActive(true);
    52.         }
    53.         if (friendly == false)
    54.         {
    55.             if (Input.GetMouseButtonDown(1) && hasSpawned == false)
    56.             {
    57.                 Spawn();
    58.                 hasSpawned = true;
    59.                
    60.                
    61.             }
    62.  
    63.             numEnemies = cb.integer_Value_we_Want;
    64.          
    65.         }
    66.         if (friendly == true)
    67.         {
    68.             if (Input.GetMouseButtonDown(1) && hasSpawned == false)
    69.             {
    70.                 Spawn();
    71.                 hasSpawned = true;
    72.             }
    73.            
    74.             numFriendlies = cb.integer_Value_we_Want2;
    75.             if (scienario)
    76.             {
    77.                 numFriendlies = numFriendlies;
    78.             }
    79.         }
    80.     }
    81.  
    82.  
    83.     void Spawn()
    84.     {
    85.         if (friendly == false)
    86.         {
    87.             for (int i = 0; i * 2 < numEnemies; i++)
    88.             {
    89.                 spawnPos = transform.position + Random.insideUnitSphere * spawnRadius;
    90.                 Instantiate(objectToSpawn, spawnPos, Quaternion.identity);
    91.             }
    92.         }
    93.         if (friendly == true)
    94.         {
    95.             for (int i = 0; i < numFriendlies; i++)
    96.             {
    97.                 spawnPos = transform.position + Random.insideUnitSphere * spawnRadius;
    98.                 Instantiate(objectToSpawn, spawnPos, Quaternion.identity);
    99.             }
    100.         }
    101.    
    102. }
    103.  
    104.  
    105. }
    106.  
     
  2. sylon

    sylon

    Joined:
    Mar 5, 2017
    Posts:
    246
    Not sure without trying, but i notice something..

    You are using Random.insideUnitSphere. This is a Vector3.
    This you add to the transform.position of the object this script is attached to.
    If you want to exclude the y-axis from your random position, you could do something like:
    Code (CSharp):
    1. Vector3 randomPos=Random.insideUnitSphere * spawnRadius;
    2. randomPos.y=0;
    3. spawnPos = transform.position + randomPos;
    Hope it helps.
     
  3. PhantomProgramming

    PhantomProgramming

    Joined:
    Jan 16, 2019
    Posts:
    60
    Thank you so much, this fixed it! Would you be interested in seeing the game? I have a gamejolt page for it if you want a link if not, that's fine too.
     
  4. sylon

    sylon

    Joined:
    Mar 5, 2017
    Posts:
    246
    Sure. Always love to see others work.
     
unityunity