Search Unity

Move newly spawned object to fixed point with Navmesh

Discussion in 'Navigation' started by Spoon_Games, Apr 10, 2021.

  1. Spoon_Games

    Spoon_Games

    Joined:
    Apr 10, 2021
    Posts:
    3
    Hello guys,

    ive pretty recently started developing using Unity. Im not all to coding at all, but its not my daily business, so im trying to learn.
    Ive started with the Tower Defense Tutorial from Brackeys. I'm at a point where from game mechanics i want to go another way.
    The goal should be to spawn an object and let the object find the way to the target destination via NavMesh. Some may know LineTowerWars from old WC3 days. Something like this where you build your own map layout by placing towers on a grid.

    I have 2 scripts for this, one is the Enemy Script and then a WaveSpawner Script.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AI;
    3. using System.Collections;
    4.  
    5. public class Enemy : MonoBehaviour {
    6.  
    7.     //public float speed = 10f;
    8.  
    9.     //private Transform target;
    10.     //private int wavepointIndex = 0;
    11.  
    12.     public Transform Ziel;
    13.     NavMeshAgent agent;
    14.     private Vector3 Entfernung;
    15.  
    16.     void Start() {
    17.     //target = Waypoints.points[0];
    18.     agent = GetComponent<NavMeshAgent>();
    19.     agent.SetDestination(Ziel.position);
    20.     }
    21.  
    22.     void Update () {
    23.         agent = GetComponent<NavMeshAgent>();
    24.         agent.SetDestination(Ziel.position);
    25.         CheckDestinationReached();
    26.     }
    27.  
    28.     void CheckDestinationReached() {
    29.         if (Vector3.Distance(Ziel.position, transform.position) <= 1.5f) {
    30.             Destroy(gameObject);
    31.             return;
    32.         }
    33.      
    34.     }
    35. }
    36. /*    void Update ()
    37.     {
    38.         Vector3 dir = target.position - transform.position;
    39.         transform.Translate(dir.normalized * speed * Time.deltaTime , Space.World);
    40.  
    41.         if (Vector3.Distance(transform.position, target.position) <= 0.5f){
    42.             GetNextWaypoint();
    43.         }
    44.     }
    45.  
    46.     void GetNextWaypoint() {
    47.         if (wavepointIndex >= Waypoints.points.Length - 1){
    48.             Destroy(gameObject);
    49.             return;
    50.         }
    51.  
    52.         wavepointIndex++;
    53.         target = Waypoints.points[wavepointIndex];
    54.     }
    55.     */
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4. using UnityEngine.AI;
    5.  
    6. public class WaveSpawner : MonoBehaviour
    7. {
    8.     public Transform enemyPrefab;
    9.  
    10.     public Transform spawnPoint;
    11.  
    12.     public float timeBetweenWaves = 5f;
    13.     private float countdown = 2f;
    14.  
    15.     public Text waveCountdownText;
    16.  
    17.     private int waveIndex = 0;
    18.  
    19.     void Update() {
    20.         if (countdown <= 0f){
    21.             StartCoroutine(SpawnWave());
    22.             countdown = timeBetweenWaves;
    23.         }
    24.         countdown -= Time.deltaTime;
    25.  
    26.         waveCountdownText.text = Mathf.Floor(countdown).ToString();
    27.     }
    28.  
    29.     IEnumerator SpawnWave() {
    30.         waveIndex++;
    31.         for (int i = 0; i < waveIndex; i++){
    32.             SpawnEnemy();
    33.             yield return new WaitForSeconds(0.5f);
    34.         }
    35.      
    36.     }
    37.  
    38.     void SpawnEnemy() {
    39.         Instantiate(enemyPrefab, spawnPoint.position, spawnPoint.rotation);
    40.     }
    41.  
    42. }
    43.  
    I've set the target destination (Ziel) in the Inspector and if i create an Enemy Object before starting the game it acts perfectly as i want it. I can create a Maze with my turrets on an open space and the enemy does find its way through it in runtime.
    But now the problem comes. If an enemy Object is spawned by the WaveSpawner it does not move at all.
    I'm kinda stuck at this place. A newly spawned Enemy does look absolutely the same to me (for example within the Inspector etc) as the beforehands spawned enemy.
    Ive been looking for a solution for 6 hours now and cant find it. I think i need to add somethin to the SpawnEnemy function but i am really not sure. What am i missing?

    Thanks for your help :) <3
     
    Last edited: Apr 10, 2021
  2. Spoon_Games

    Spoon_Games

    Joined:
    Apr 10, 2021
    Posts:
    3
    Edit: Okay i've found something out: If i pause and drag the finish location into place on inspector view for a new spawned enemy object it does start to move after pausing. So i need to find a way to auto assign the finish location right after spawning it, am i right?
     
  3. Spoon_Games

    Spoon_Games

    Joined:
    Apr 10, 2021
    Posts:
    3
    Solved. This Thread can be deleted.

    I simply changed the gameobject i created that shall function as a target destination to a cube. Made this cube really small and this now works!