Random spawning - min Distance and max Distance

Hi everybody i am making a top down space shooter.

Atm i am trying to instantiate a comet object - it should spawn every three second and spawn at a point between the minDist and the maxDist. For some reason it is not doing that. Anyone got any solutions or suggestions?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RandomSpawn : MonoBehaviour
{

    public float minDist = 10f;
    public float maxDist = 20f;
    public GameObject objectToSpawn;
    public Transform Player;
    float Timer;

    void Start ()
    {
        Timer = Time.time + 3;
    }

    void Update()
    {

        float distance = Vector3.Distance(transform.position, Player.position);

        if (distance >= minDist && distance <= maxDist && Timer < Time.time)
        {
            Debug.Log("Hello");
            Instantiate(objectToSpawn, transform.position, transform.rotation);
            Timer = Time.time + 3;
        }

    }
}
1 Like

To what I can see, the only instance of minDist and maxDist are in the condition, that checks if the distance is bigger than the minimum, lesser than the maximum and at least 3 seconds since the last time this conditions were met, but in the Instantiate, where you spawn the object, you are doing it at a constant position, I mean, you are instatiating it in the same place the object this script is attached to is.

As its a top-down space shooter, I assume is 2D, and the Z of the objects is always 0, so the only random ones must be x and y, depending on what axis you desire to make the objectToSpawn randomly appear is where you should place the Random.Range():

If you want to it spawn randomly in the x axis:

Instantiate(objectToSpawn, new Vector3(Random.Range(minDist, maxDist), transfom.position.y, 0), transform.rotation)

If you want to it spawn randomly in the y axis:

Instantiate(objectToSpawn, new Vector3(transform.position.x, Random.Range(minDist, maxDist), 0), transform.rotation)

*Note these examples make it spawn at the same Y or X the object the script is attached to

and If you want it to spawn in a random place of the screen, rather on a random place on an axis, you should try:

Instantiate(objectToSpawn, new Vector3(Random.Range(minDist, maxDist), Random.Range(minDist, maxDist), 0), transform.rotation)

I could give you a more accurate suggestion if these dosent fit what you want, but Ill need you to specify a bit more.

For some reason the following code does not spawn anything at all.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RandomSpawn : MonoBehaviour
{

    public float minDist = 10f;
    public float maxDist = 20f;
    public GameObject objectToSpawn;
    float Timer;
    public Transform Player;

    void Start ()
    {
        Timer = Time.time + 1;
    }

    void Awake ()
    {
        Player = GameObject.FindGameObjectWithTag("Player").transform;
    }

    void Update()
    {
        float distance = Vector3.Distance(transform.position, Player.position);

        if (distance >= minDist && distance <= maxDist && Timer < Time.time)
        {
            Instantiate(objectToSpawn, new Vector3(transform.position.x, Random.Range(minDist, maxDist), 0), transform.rotation);
            Timer = Time.time + 1;
        }

    }
}

I dont quite know what you want to do, but what you are doing here is:

*That will happen once every second

Why dont you instead try:

[code=CSharp]        if (Timer < Time.time)
        {
            Instantiate(objectToSpawn, new Vector3(transform.position.x, Random.Range(minDist, maxDist), 0), transform.rotation);
            Timer = Time.time + 1;
        }

Also, does it give any error?

Its alright bro and thanks for the help! I figured it out. I will post the code and solution when i get my laptop back :wink: :slight_smile: