Placing gameobjects at random locations

mattyg55

Member
Joined
Apr 28, 2023
Messages
8
Programming Experience
Beginner
Hello, I am a student trying to code a basic game for an intro to coding class. I am creating a game where a car is driving down the road and gameobjects (cones) are falling from the sky onto the road at random places. I was able to code the car and road to move but could not code the cones gameobjects to fall from the sky. I need the cones to fall at random locations in front of the car at random speeds. Would someone be able to guide me as to how to write the commands to get these gameobjects to appear?
 
The cones falling from the sky would be similar logic to the car moving on the road: every few seconds in your game loop, you just advance the position of the cone closer to the ground. When the cone "hits" the roads, then it stops falling.

You would just use a random number generator to generate a number that maps to the random location in the sky for the initial position of the cone, as well as generate a random velocity for the cone's fall rate.
 
The cones falling from the sky would be similar logic to the car moving on the road: every few seconds in your game loop, you just advance the position of the cone closer to the ground. When the cone "hits" the roads, then it stops falling.

You would just use a random number generator to generate a number that maps to the random location in the sky for the initial position of the cone, as well as generate a random velocity for the cone's fall rate.

Thanks for your response Skydiver! I am trying to get multiple cones to display at random times and locations which is a different outcome from what the moving car is supposed to do. This is the code I have so far based on what I did in my class but I am not sure what I am missing. I also need to automate the command to start automatically when the scene starts.

Thanks in advance.

C#:
    void Start()
    {
        cone = GameObject.Find("Traffic_Cone");
        cone.GetComponent<Rigidbody>().useGravity = false;

        if (Time.time - prevTime >= interval && startGame == true)
        {
            cone.GetComponent<Rigidbody>().useGravity = true;
            cone.GetComponent<Rigidbody>().drag = 0;

            foreach (GameObject cone in conelist)
            {
                cone.GetComponent<Rigidbody>().useGravity = true;
                cone.GetComponent<Rigidbody>().drag = 0;
            }


            foreach (GameObject conebject in conelist)
            {
                float posx = Random.Range(-5, 5);
                float posy = Random.Range(1, 4);
                float posz = Random.Range(-5, 5);

                cone.gameObject.transform.position = new Vector3(posx, posy, posz);

                cone.GetComponent<Rigidbody>().useGravity = false;
                cone.GetComponent<Rigidbody>().drag = 100;
            }
        }
    }
 
I ended up modifying the code based on another idea but now all the cones are appearing at one time in a tight cluster. Would someone please be able to help me understand what the issue is and how to get the cones to appear at different locations at different times? Thank you!
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class placeCones : MonoBehaviour
{
    public GameObject[] cone;
    public float[] dropTimes;
    public float minX, minZ, maxZ;

    private int currentObjectIndex = 0;

    private void Start()
    {
        Invoke("DropCone", dropTimes[currentObjectIndex]);
    }
    private void DropCone()
    {
      
            Vector3 randomSpawnPosition = new Vector3(Random.Range(-50, 50), 0, Random.Range(0, 200));
            Instantiate(cone[currentObjectIndex],randomSpawnPosition, Quaternion.identity);

        currentObjectIndex++;
        if (currentObjectIndex >= cone.Length)
        {
            currentObjectIndex = 0;
        }

        Invoke("DropObject", dropTimes[currentObjectIndex]);
    }

}
 
Instead of using a fixed array that has the drop time delays, just compute a random delay.
 
Nothing life changing. Just change from:
C#:
Invoke("DropCone", dropTimes[currentObjectIndex]);
:
Invoke("DropObject", dropTimes[currentObjectIndex]);
to
C#:
Invoke("DropCone", Random.Range(MinDelay, MaxDelay));
:
Invoke("DropObject", Random.Range(MinDelay, MaxDelay));

It's just a matter of understanding the code that you are using because presumably your dropTimes[] array contained some random or pre-computed values and all you were doing was just cycling over those values. The only change is just to let the randomizer compute the values on the fly.
 
Nothing life changing. Just change from:
C#:
Invoke("DropCone", dropTimes[currentObjectIndex]);
:
Invoke("DropObject", dropTimes[currentObjectIndex]);
to
C#:
Invoke("DropCone", Random.Range(MinDelay, MaxDelay));
:
Invoke("DropObject", Random.Range(MinDelay, MaxDelay));

It's just a matter of understanding the code that you are using because presumably your dropTimes[] array contained some random or pre-computed values and all you were doing was just cycling over those values. The only change is just to let the randomizer compute the values on the fly.

I made those changes but am getting errors for those lines where the MinDelay and MaxDelay values are. Is there something else I am missing (See attached code)
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class placeCones : MonoBehaviour
{
    public GameObject[] cone;
    //public float[] MinDelay, MaxDelay;
    public float minX, minZ, maxZ;

    private int currentObjectIndex = 0;

    private void Start()
    {
        Invoke("DropCone", Random.Range(MinDelay, MaxDelay));
    }
    private void DropCone()
    {

        Vector3 randomSpawnPosition = new Vector3(Random.Range(-50, 50), 0, Random.Range(0, 200));
        Instantiate(cone[currentObjectIndex], randomSpawnPosition, Quaternion.identity);

        currentObjectIndex++;
        if (currentObjectIndex >= cone.Length)
        {
            currentObjectIndex = 0;
        }

        Invoke("DropObject", Random.Range(MinDelay,MaxDelay));
    }

}
?
 
The MinDelay and MaxDelay would be constants that you define. Again: you need to understand the code that you are using. Don't just copy and paste in code. If you are planning on just copying and pasting in code, you might as well just stop learning, and ask ChatGPT to start writing your code for you.

As a quick aside, I don't do Unity programming. I'm just looking at the code that you are presenting and commenting on where I can see improvements.
 
Back
Top Bottom