Search Unity

Trying to get each of my customers to move randomly individually at the same time

Discussion in '2D' started by GordonArber, Feb 15, 2019.

  1. GordonArber

    GordonArber

    Joined:
    Jan 2, 2016
    Posts:
    22
    Hi I am trying to get each of my customers to move randomly individually at the same time in a list. Any help would be greatly appreciated!
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CustomerPoolManager : MonoBehaviour
    6. {
    7.     //What object to instantiat
    8.     public GameObject customerPrefab;
    9.     public int spawnCount;
    10.     //list of objects that can be used
    11.     public List<GameObject> customerList;
    12.    
    13.  
    14.     private float customerXPos = -4.11f;
    15.     private float customerYPos = -12.5f;
    16.  
    17.     public float speed;
    18.     private float dazedTime;
    19.     public float startDazedTime;
    20.  
    21.     private float waitTime;
    22.     public float startWaitTime;
    23.  
    24.     public Transform[] storeMoveSpots;
    25.     private int randomSpot;
    26.     public float kitchenNoise;
    27.  
    28.     private void Start()
    29.     {
    30.         waitTime = startWaitTime;
    31.         randomSpot = Random.Range(0, storeMoveSpots.Length);
    32.  
    33.         for (int i = 0; i < spawnCount; i++)
    34.         {          
    35.             GameObject customerSpawned = (GameObject)Instantiate(customerPrefab, new Vector2(customerXPos, customerYPos), Quaternion.identity);
    36.             customerList.Add(customerSpawned);
    37.             customerSpawned.transform.parent = this.transform;
    38.             customerSpawned.SetActive(true);
    39.            
    40.         }
    41.     }
    42.     private void Update()
    43.     {      
    44.         foreach (GameObject customerPrefab in customerList)
    45.         {
    46.             customerPrefab.transform.position = Vector2.MoveTowards(customerPrefab.transform.position, storeMoveSpots[randomSpot].position, speed * Time.deltaTime);
    47.  
    48.             if (Vector2.Distance(customerPrefab.transform.position, storeMoveSpots[randomSpot].position) < 0.2f)
    49.             {
    50.                 if (waitTime <= 0)
    51.                 {
    52.                     randomSpot = Random.Range(0, storeMoveSpots.Length);
    53.                     waitTime = startWaitTime;
    54.                 }
    55.                 else
    56.                 {
    57.                     waitTime -= Time.deltaTime;
    58.                 }
    59.             }
    60.         }
    61.     }
    62. }
    63.  
     
  2. GordonArber

    GordonArber

    Joined:
    Jan 2, 2016
    Posts:
    22
    I think I am making progress, but it's moving the list and not the individual items in the list. Do I have to add "new" somewhere?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CustomerPoolManager : MonoBehaviour
    6. {
    7.     //What object to instantiat
    8.     public GameObject customerPrefab;
    9.     public int spawnCount;
    10.     //list of objects that can be used
    11.     public List<GameObject> customerList = new List<GameObject>();
    12.  
    13.  
    14.     private float customerXPos = -4.11f;
    15.     private float customerYPos = -12.5f;
    16.  
    17.     public float speed;
    18.     private float dazedTime;
    19.     public float startDazedTime;
    20.  
    21.     private float waitTime;
    22.     public float startWaitTime;
    23.  
    24.     public Transform[] storeMoveSpots;
    25.     private int randomSpot;
    26.     public float kitchenNoise;
    27.  
    28.     private void Start()
    29.     {
    30.         waitTime = startWaitTime;
    31.         randomSpot = Random.Range(0, storeMoveSpots.Length);
    32.  
    33.         for (int i = 0; i < spawnCount; i++)
    34.         {
    35.             GameObject customerSpawned = (GameObject)Instantiate(customerPrefab, new Vector2(customerXPos, customerYPos), Quaternion.identity);
    36.             customerList.Add(customerSpawned);
    37.             customerSpawned.transform.parent = this.transform;
    38.             customerSpawned.SetActive(true);
    39.  
    40.         }
    41.     }
    42.     private void Update()
    43.     {
    44.         Debug.Log(customerList);
    45.         foreach (GameObject cLst in customerList)
    46.         {
    47.             cLst.transform.position = Vector2.MoveTowards(cLst.transform.position, storeMoveSpots[randomSpot].position, speed * Time.deltaTime);
    48.  
    49.             if (Vector2.Distance(cLst.transform.position, storeMoveSpots[randomSpot].position) < 0.2f)
    50.             {
    51.                 if (waitTime <= 0)
    52.                 {
    53.                     randomSpot = Random.Range(0, storeMoveSpots.Length);
    54.                     waitTime = startWaitTime;
    55.                 }
    56.                 else
    57.                 {
    58.                     waitTime -= Time.deltaTime;
    59.                 }
    60.             }
    61.         }
    62.     }
     
  3. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You instantiate your prefabs with customerXPos and customerYpos transform position. I imagine this position will be exactly what you hard coded them to be so at some point you should update those variables to the new transform x and y positions.

    When your storeSpot transform is chosen, maybe do something like

    Code (CSharp):
    1. customerXPos = storeSpot.x;
    2. customerYPos = storeSpot.y;