Why my minimum distance in Unity not working?

distance to unity
unity compute distance
distance to point unity
checking distance unity
unity3d vector distance
finding distance in unity
distance squared unity
unity find distance between objects

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);
        }
    }
}


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

How to determine the minimum distance between objects?, The problem is that if there are many objects, their position change is visible. I.e, if the object is loaded on the second object, you can see how it  Why is my DistanceJoint2D not working? I am trying to create a grappling mechanism with two objects A and B where A is the player and B is the grapple. I connected them with a distance joint so that I can control the length of the grapple.


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);
    }
}

Why min distance not working?, I have a "Gear" object that spawns. I have determined the minimum and max distance that can be between objects. Hhowever, objects still  Submission failed. For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.


Hopefully I understand your code right: you're trying to create a chain of gears from bottom to top by moving a random amount upwards, then a random amount left/right. If that's correct, then you might be overthinking it.

Rect boundaries;

void Start()
{
    boundaries = new Rect(
        transform.position, //corner 1
        maxWidthPoint.position - transform.position //size: corner 2 - corner 1
    );
}

void Update()
{
    if (transform.position.y < generationPoint.position.y)
    {
        float deltaY = Random.Range(distanceBetweenMin, distanceBetweenMax);
        float deltaX = Random.Range(minWidthChange, maxWidthChange);
        transform.position += new Vector3(deltaX, deltaY, 0);

        if(transform.position.y < boundaries.yMin)
            transform.position.y = boundaries.yMin
        if(transform.position.y > boundaries.yMax)
            transform.position.y = boundaries.yMax

        if(transform.position.x < boundaries.xMin)
            transform.position.x = boundaries.xMin
        if(transform.position.x > boundaries.xMax)
            transform.position.x = boundaries.xMax

        int index = Random.Range(0, theGears.Length);
        Instantiate(theGears[index], transform.position, transform.rotation);
    }
}

Trying to find the minimum Vector3.distance from an array, I've been directing people there for a while now. Click to expand Not a problem. I open up the script and there's red notches all up and down it  Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.


Solved

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;
}



public void Update (){

    if (transform.position.y < generationPoint.position.y)
    {
        distanceBetween = Random.Range (distanceBetweenMin, distanceBetweenMax) + 0.5f;

        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);
    }

} }

distance shortest in the list, You need to iterate over the list of GameObjects, then, for each GO, iterate over all GOs with a higher index and compare the distance. Unity can only calculate shadows for materials with shaders that support lighting. In the Built-in Render Pipelne, using the Forward rendering path , some shaders allow only the brightest directional light to cast shadows (in particular, this happens with Unity’s legacy built-in shaders from 4.x versions).


Keeping distance between two gameObjects, However, the current way that I am attempting to do it spits out an error: Your problem is assigning a distance (scalar) to a vector. What you  Calculates the minimum distance between two colliders. A valid colliderA and colliderB must be provided for the returned ColliderDistance2D to be valid i.e. both colliderA or colliderB should not be NULL, should not be disabled and must contain collision shapes.


How to keep the distance between objects so that they do not collide , https://docs.unity3d.com/Manual/ExecutionOrder.html But the most important and main problem is that the objects still do not keep If you oversize the trigger, then that will represent half the minimum distance apart. What that code SHOULD do is not only set MarkedForDeath on any enemy I hit to True (Which only seems to work if I check the box for the enemy in the unity editor, not even if I set it to true at the top of enemy script, but that's another issue), but as you can see I set it to log any collision. The log is empty.


Minimum distance on configurable joint?, One quick solution that I would test as well is to put hard limits using colliders, this might be sufficient for your problem. But be careful, the two  The minimum distance between points in the trail, in world units. AutoDestruct: Enable this to destroy the GameObject to which the Trail Renderer component is attached after it has not moved for Time seconds. Emitting: When this is enabled, Unity adds new points in the trail. When this is disabled, Unity does not add new points to the trail.