Physics.CheckSphere(及类似产品)并非总是有效

时间:2019-07-03 09:19:06

标签: c# unity3d

我正在生成球体,并希望它们不相交。该脚本通常可以正常工作,但我总是能使几个领域相交-彼此成对或成对地相交。

我尝试了所有方法-CheckSphereOverlapSphere,也对boxmesh使用相同的方法,尝试将触发点上的位置与另一个球体随机化-没有帮助。

void CreateObject(Vector3 v)
{
    _myObj = Instantiate(ballPrefab, _cloneParent);
    _myObj.transform.localPosition = v;
    _myObj.GetComponent<MeshRenderer>().enabled = false;
    SpawnedObjects.Append(_myObj);
}

SpawnedObjects = new GameObject[_amountRandomized];

_cloneParent = GameObject.FindGameObjectWithTag("SurfaceFlag").transform;
const float objectSize = 105f;
for (var i = 0; i < _amountRandomized; i++)
{
    var randomizedPos = new Vector3(Random.Range(-DistanceFromCenter, DistanceFromCenter), SpawnDepth, Random.Range(-DistanceFromCenter, DistanceFromCenter));
    while (Physics.CheckSphere(_cloneParent.transform.TransformPoint(randomizedPos), objectSize, 10))
    {
        Debug.Log(Physics.CheckSphere(_cloneParent.transform.TransformPoint(randomizedPos), objectSize, 10));
        randomizedPos = new Vector3(Random.Range(-DistanceFromCenter, DistanceFromCenter), SpawnDepth, Random.Range(-DistanceFromCenter, DistanceFromCenter));
    }

    CreateObject(randomizedPos);
}

private void OnTriggerEnter(Collider col)
{
    if (col.gameObject.CompareTag("Collectible"))
    {
        Debug.Log("Moving sphere");
        _transform.position = new Vector3(Random.Range(-_distanceFromCenter, _distanceFromCenter), Gameplay.SpawnDepth, Random.Range(-_distanceFromCenter, _distanceFromCenter));
    }
    _goDown = false; //trigger to move 6f downwards with Lerp
    _goUp = true; //same but upwards
    _startTime = Time.time;
}

未收到任何错误消息。

0 个答案:

没有答案