2015-01-20 3 views
0

작은 Unity2D 게임을 개발 중이며 기본적으로 두 개의 동전을 인스턴스화 할 여유 공간을 확인하려고합니다. 나는 코 루틴을 만들었지 만 웬일인지 그들이 어떤 경계선에 도달했는지 확인하면서 동전을 인스턴스화하지는 않습니다.빈 공간에 동전을 인스턴스화

이 내 코 루틴 어떤 도움이 대단히 이해할 수있을 것이다

private IEnumerator InsCoins(float secondsBetweenEachIns) 
{ 
    while (true) 
    { 
     float tmp1X = float.Parse(Random.Range(minX, maxX).ToString()); 
     float tmp1Y = float.Parse(Random.Range(minX, maxX).ToString()); 
     float tmp2X = float.Parse(Random.Range(minX, maxX).ToString()); 
     float tmp2Y = float.Parse(Random.Range(minX, maxX).ToString()); 

     Vector3 ins1 = new Vector3(tmp1X, tmp1Y); 
     Vector3 ins2 = new Vector3(tmp2X, tmp2Y); 

     int count = GameObject.FindObjectsOfType<GameObject>().Length; 
     int arrayCount = 0; 
     Bounds[] getBound = new Bounds[count]; 
     foreach (GameObject go in GameObject.FindObjectsOfType<GameObject>()) 
     { 
      getBound[arrayCount] = go.renderer.bounds; 
      arrayCount++; 
     } 
     GameObject tmp1 = (GameObject)GameObject.Instantiate(coin, new Vector3(tmp1X, tmp1Y, 0), Quaternion.identity); 
     GameObject tmp2 = (GameObject)GameObject.Instantiate(redcoin, new Vector3(tmp2X, tmp2Y, 0), Quaternion.identity); 

     foreach (Bounds go in getBound) 
     { 
      do 
      { 
       if (go.Intersects(tmp1.renderer.bounds) || go.Intersects(tmp2.renderer.bounds)) 
       { 
        Destroy(tmp1); Destroy(tmp2); 
       } 
      } while (!go.Intersects(tmp1.renderer.bounds) && go.Intersects(tmp2.renderer.bounds)); 
     } 
     yield return new WaitForSeconds(secondsBetweenEachIns); 
    } 
} 

입니다!

답변

2

아마도 코 루틴을 올바르게 호출하지 않은 것일 수 있습니다. 어디서 어떻게 InsCoins에 전화 하시겠습니까?

이 같은 전화를 그냥했습니다이 코 루틴을 호출 할 경우 : 당신이 말한대로 것을하고 있어요,하지만 아무것도 작동하지

StartCoroutine(InsCoins(2.0)); // For InsCoins with 2 sec between each

+0

예. –

+0

아마 당신이하고있는 일의 접근 방식이 올바르지 않을 수 있습니다. "여유 공간을 확인하고 싶다"는 것은 무엇을 의미합니까? 정의 된 공간이 게임 개체에 의해 점유되지 않을 때 두 개의 동전을 만들고 싶다고 생각했습니다. 그 맞습니까? 좀 더 많은 정보를 주시면 더 나은 접근 방법을 찾을 수 있도록 도와 줄 수 있습니다. – Unai

+0

예. 나는 콜리더로 설정된 벽을 가지고 있으며 플레이어가 그 벽에 도달 할 수 없기 때문에 그 벽에 동전이 인스턴스화되는 것을 원하지 않습니다. 동전이 게임 공간이없는 자유 공간에있게하고 싶습니다. –

관련 문제