Sud*_*ila 5 unity-game-engine unityscript
我想在我的屏幕中随机生成气泡.当气泡在一个地方产生时,其他气泡不能在其半径1区域附近产生.表示气泡不会与任何其他气泡碰撞或触发.
我该怎么做 ?
public void GenerateBubble ()
{
newBubbleXPos = Random.Range (-7, 7);
newBubbleYPos = Random.Range (-3, 3);
bubbleClone = (GameObject)Instantiate (bubblePrefab, new Vector3 (newBubbleXPos, newBubbleYPos, 0), Quaternion.identity);
UIManager.instance.ChangeBubbleSprite (bubbleClone);
bubbleList.Add (bubbleClone);
if (bubblePosList.Contains (bubbleClone.transform.position)) {
bubbleClone.transform.position=new Vector3(Random.Range (-7,7),Random.Range (-3,3),0);
}
bubblePosList.Add (bubbleClone.transform.position);
bubbleClone.transform.parent = UIManager.instance.CurrentLevel.transform;
GLOBALS.bubbleCounter++;
}
Run Code Online (Sandbox Code Playgroud)
在我的代码中,每个气泡都生成在不同的位置,但它可以与其他气泡碰撞,我想生成新的气泡不同的位置,也不会碰撞.我的泡泡对手的半径是1.
我找到了答案:
public List<GameObject> bubbleList = new List<GameObject> ();
private int newBubbleXPos;
private int newBubbleYPos;
public void GenerateBubble ()
{
bool locationInvaild = true;
while (locationInvaild) {
newBubbleXPos = Random.Range (-8, 8);
newBubbleYPos = Random.Range (-4, 4);
currentPosition = new Vector3 (newBubbleXPos, newBubbleYPos, 0);
locationInvaild = false;
for (int i=0; i<bubbleList.Count; i++) {
if (Vector3.Distance (bubbleList [i].transform.position, currentPosition) < 2.5f * radius) {
locationInvaild = true;
break;
}
}
}
bubbleClone = Instantiate (bubblePrefab, new Vector3 (newBubbleXPos, newBubbleYPos, 0), Quaternion.identity) as GameObject;
bubbleList.Add (bubbleClone);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
627 次 |
| 最近记录: |