Search Unity

Resolved Next GameObject In Array

Discussion in 'Scripting' started by WildLife_Studios, Feb 15, 2024.

  1. WildLife_Studios

    WildLife_Studios

    Joined:
    Oct 29, 2023
    Posts:
    3
    I'm a beginning developer and I followed Blackthornprod's 'PATROL AI WITH UNITY AND C# - EASY TUTORIAL' for one of my projects to understand a bit more about AI, but I need the enemy to go to the next gameobject in the array, instead of it being random. I can't find any answers online and I need help, here's the script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Patroll : MonoBehaviour
    6. {
    7.     public float speed = 5f;
    8.     private float waitTime;
    9.     public float startWaitTime;
    10.  
    11.     public Transform[] moveSpots;
    12.     private int randomSpot;
    13.  
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         waitTime = startWaitTime;
    19.         randomSpot = Random.Range(0, moveSpots.Length);
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         transform.position = Vector2.MoveTowards(transform.position, moveSpots[randomSpot].position, speed * Time.deltaTime);
    26.  
    27.         if(Vector2.Distance(transform.position, moveSpots[randomSpot].position) < 0.2f)
    28.         {
    29.             if(waitTime <= 0)
    30.             {
    31.                 randomSpot = Random.Range(0, moveSpots.Length);
    32.                 waitTime = startWaitTime;
    33.             }
    34.             else
    35.             {
    36.                 waitTime -= Time.deltaTime;
    37.             }
    38.         }
    39.     }
    40. }
    41.  
    I think it's something got to do with 'randomSpot = Random.Range(0, moveSpots.Length)'
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,698
    The answer you seek lies in any waypoint patrolling tutorial.

    Here's the essence:

    It will involve keeping an integer index into which node you go to next.

    You will move towards that node, looking up its position via the index.

    When you reach it, you will increment index.

    If it exceeds the number of items, set it back to zero.
     
    Nad_B and SisusCo like this.
  3. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Well, you're correct that it's on that line. All you would need to do is change it from random to incrementing.
     
    Nad_B and Kurt-Dekker like this.
  4. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    730
    So basically, what @Kurt-Dekker and @RadRedPanda say, is to first rename randomSpot to currentSpot (it's always a good idea (re)name variables to match their functionality). Then just do a currentSpot = currentSpot + 1 (or can be abbreviated to just currentSpot++), then immediately check if currentSpot exceeds your number of spots - 1 (since arrays indices start at 0, not 1), and if it does, revert it to 0 to loop over.
     
    WildLife_Studios and SisusCo like this.
  5. WildLife_Studios

    WildLife_Studios

    Joined:
    Oct 29, 2023
    Posts:
    3
    Thank you guys for your time, I got it to work.
     
    Kurt-Dekker and Nad_B like this.
  6. WildLife_Studios

    WildLife_Studios

    Joined:
    Oct 29, 2023
    Posts:
    3
    Thank you @Nad_B this helped a-LOT
     
    Nad_B likes this.