我正在生成球体,并希望它们不相交。该脚本通常可以正常工作,但我总是能使几个领域相交-彼此成对或成对地相交。
我尝试了所有方法-CheckSphere
,OverlapSphere
,也对box
和mesh
使用相同的方法,尝试将触发点上的位置与另一个球体随机化-没有帮助。
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;
}
未收到任何错误消息。