为什么我在 Unity 中的最小距离不起作用?

Why my minimum distance in Unity not working?

UNITY 2D C#

我有一个生成的 "Gear" 对象。

我已经确定了对象之间的最小和最大距离。

但是,对象仍然会生成在其他对象上,覆盖(重叠)它们。

如何修改?

我的脚本:

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

我猜你的物体的大小不完全是一个单位,请检查有问题的物体的比例并相应地调整你的最小距离

好吧,我不确定我是否完全理解您的代码应该做什么。 不过有几个地方看起来有点可疑,我根据我对它的预期用途的理解进行了更改(标记为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);
    }
}

希望我理解你的代码是正确的:你试图通过向上移动一个随机量,然后是一个随机量 left/right 来创建从下到上的齿轮链。如果这是正确的,那么你可能想多了。

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

已解决

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

} }