- Home /
How Do I Procedurally Generate Clouds ?
Hello Community!
I am giving procedural generation a crack. the goal is to instantiate planes (clouds) in a random area on screen.
In my case 0 - 480 on the x - axis, and 0 - 360 on the y - axis.
The script i'm using is this.
Script.js:
#pragma strict
var blueCloud : GameObject;
var orangeCloud : GameObject;
var cloudChance : int;
var cloudPosition : Vector3;
var cloudX : float;
var cloudY : float;
function Start () {
cloudChance = Random.Range (1, 3);
if (cloudChance == 1) {
cloudX = Random.Range(0, 4.8);
cloudY = Random.Range(0, 3.2);
cloudPosition = Vector3(cloudX, cloudY, - 0.2);
Instantiate (redCloud, cloudPosition, transform.rotation);
}
if (cloudChance == 2) {
cloudX = Random.Range(0, 4.8);
cloudY = Random.Range(0, 3.2);
cloudPosition = Vector3(cloudX, cloudY, - 0.2);
Instantiate (orangeCloud, cloudPosition, transform.rotation);
}
if (cloudChance == 3) {
cloudX = Random.Range(0, 4.8);
cloudY = Random.Range(0, 3.2);
cloudPosition = Vector3(cloudX, cloudY, - 0.2);
Instantiate (blueCloud, cloudPosition, transform.rotation);
}
}
But it only Instantiates the plane (cloud) randomly in the top right part of the screen (never behind or underneath the player).
Thanks for the interest so far!
Are your clouds geometry at the origo of the prefab root?
You are setting world coordinates, not screen coordinates. That's why.
@Statement: It's a plane with a GUITexture, so I'm not sure.
@$$anonymous$$ryptos: world and screen Co-ordinates aren't making any difference.
Answer by aldonaletto · Mar 30, 2012 at 12:56 PM
The position property in GUIText and GUITexture is in viewport coordinates: 0,0 is lower left, 1,1 is top right. You must specify valid x and y coordinates (between 0 and 1). The z coordinate sets its depth relative to other GUIText or GUITexture objects (higher z appears over the others). If the GUI element is a child of another object, you must set its position after the instantiation; if the parent object moves, you must set the gui position each Update (any small parent movement shifts the gui object out of screen).
@AldoNaletto: Thank you!
sorry it's taken me so long to reply. I just thought i'd mention that I am using decimals because regular co-ordinates aren't working (4.8 ins$$anonymous$$d of 480 and 3.6 ins$$anonymous$$d of 360, no idea why!).
I can't seem to spawn the cloud randomly on the whole screen, with iphone screen resolution.
Your answer