2016-04-10 21 views
0

aus dem Bildschirm hervorgebracht. Ich lerne an meinem ersten 2D-Spiel in Unity auf einem Tutorial und ich traf ein Problem, das ich nicht lösen kann. In diesem Spiel gibt laichen Wolken zufällig, die auf dem Bildschirm bleiben muss, und dies wird in CloudSpawner Skript geschrieben:Objekte werden in Unity

I MaxX und MinX entsprechend der Kameragröße eingestellt:

void setMinAndMaxX(){ 

     Vector3 bounds = Camera.main.ScreenToWorldPoint (new Vector3(Screen.width, Screen.height, 0)); 

     minX = -bounds.x + 0.5f; 
     maxX = bounds.x - 0.5f; 

    } 

Und hier ist die Funktion, auf jede Wolke setzt Position Array aus Wolken:

void CreateClouds(){ 

     Shuffle (clouds); 

     float positionY = 0f; 

     for (int i = 0; i < clouds.Length; i++) { 

      Vector3 temp = clouds [i].transform.position; 

      temp.y = positionY; 

      //temp.x = Random.Range (minX, maxX); 

      if (controlX == 0) { 

       temp.x = Random.Range (0, maxX); 
       controlX = 1; 

      } else if (controlX == 1) { 

       temp.x = Random.Range (0, minX); 
       controlX = 2; 

      } else if (controlX == 2) { 

       temp.x = Random.Range (1.0f, maxX); 
       controlX = 3; 

      } else if (controlX == 3) { 

       temp.x = Random.Range (-1.0f, minX); 
       controlX = 0; 

      } 

      lastCloudPositionY = positionY; 

      clouds [i].transform.position = temp; 

      positionY -= distanceBetweenClouds; 

     } 


    } 

Lassen sie mich erklären, was das eigentliche Problem hier ist - sollten die Skripte arbeiten, wie es ist, aber in Unity wenn ich drücken Tannen PAUSE t und dann PLAY drücken, spawnen die Wolken gut auf dem Bildschirm. Aber wenn ich nur PLAY drücke, werden auch Wolken aus dem Bildschirm geworfen. Ich habe das Gefühl, es gibt ein Problem mit Unity Software und vielleicht nicht mit dem Code und ich fand einen Thread, wo es heißt, dass es entgegengesetzt sein sollte (also wenn Sie Pause zuerst drücken und dann spielen könnte es zu Problemen mit dem Laichen der Wolken kommen - und nicht wenn Sie nur drücken, spielen).

Antwort

0

Ok, ich habe die Lösung gefunden. Ich verwende zur Zeit Unity Version 5.3.1f1, wo es einen kleinen Unterschied gibt mit screen.width in meiner SetMinAndMax() Funktion. Ich fügte bounds.x = Mathf.Floor(bounds.x/2); wie folgt hinzu:

void setMinAndMaxX(){ 

    Vector3 bounds = Camera.main.ScreenToWorldPoint (new Vector3(Screen.width, Screen.height, 0)); 

    bounds.x = Mathf.Floor(bounds.x/2); 

    minX = -bounds.x + 0.5f; 
    maxX = bounds.x - 0.5f; 

} 
Verwandte Themen