Why my minimum distance in Unity not working?

990 views c#
4

UNITY 2D C#

I have a "Gear" object that spawns.

I have determined the minimum and max distance that can be between objects.

However, objects still spawn on other objects, covering (overlapping) them.

How to change it?

My script:

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

public class GearsSpawner : MonoBehaviour
{

    public GameObject theGear;
    public Transform generationPoint;

    public float distanceBetween;
    public float distanceBetweenMin;
    public float distanceBetweenMax;

    private int gearSelector;
    public GameObject[] theGears;

    private float minWidth;
    public Transform maxWidthPoint;

    private float maxWidth;
    public float maxWidthChange;
    private float widthChange;

    void Start()
    {
        minWidth = transform.position.x;
        maxWidth = maxWidthPoint.position.x;
    }


    void Update()
    {
        if (transform.position.y < generationPoint.position.y)
        {
            distanceBetween = Random.Range(distanceBetweenMin, distanceBetweenMax);
            gearSelector = Random.Range(0, theGears.Length);
            widthChange = transform.position.x + Random.Range(maxWidthChange, -maxWidthChange);
            if (widthChange > maxWidth)
            {
                widthChange = maxWidth;
            }
            else if (widthChange < minWidth)
            {
                widthChange = minWidth;
            }
            transform.position = new Vector3(widthChange, transform.position.y + distanceBetween, transform.position.z);
            Instantiate(theGears[gearSelector], transform.position, transform.rotation);
        }
    }
}

enter image description here

answered question

2 Answers

9

im guessing your objects are not exactly one unit in size, check the scale of the objects in question and adjust you minimum distance accordingly

posted this
6

Well I'm not sure if I totally understand what your code is supposed to do. However there are a few spots that look fishy and I have changed them according to my understanding of its intended purpose (marked with FIX):

void Update()
{
    if (transform.position.y < generationPoint.position.y)
    {
        distanceBetween = Random.Range(distanceBetweenMin, distanceBetweenMax);
        gearSelector = Random.Range(0, theGears.Length);
        widthChange = transform.position.x + Random.Range(-maxWidthChange, maxWidthChange); // FIX: the first argument of Random.Range must be the lower limit, the second one that upper limit
        if (widthChange > maxWidth)
        {
            widthChange = maxWidth;
        }
        else if (widthChange < minWidth)
        {
            widthChange = minWidth;
        }
        Vector3 newPosition = new Vector3(widthChange, transform.position.y + distanceBetween, transform.position.z); // FIX: instead of overwriting the spawner's position, store the newPosition in a local and use that
        Instantiate(theGears[gearSelector], newPosition, transform.rotation);
    }
}

posted this

Have an answer?

JD

Please login first before posting an answer.