Click here to Skip to main content
15,890,845 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I tried the following code, but with the following code I can only create random objects on the "x" axis.

How to create a random object in the "x" and "y" axes in my 2D game.

What I have tried:

JavaScript
<pre>#pragma strict

var theObject: GameObject;
var maxXPos: float = 2000.0;
var minXPos: float = 9.0;

var maxYPos: float = 2000.0;
var minYPos: float = 9.0;

var max = 22;


function Start() {
   StartCoroutine(spawn());
  
}

function spawn(): IEnumerator {
    for (var i = 0; i < max; i++) {
        yield WaitForSeconds(2.0);
       var theNewPos = new Vector3(Random.Range(minXPos, maxXPos), 0, Random.Range(minXPos, maxXPos));
                var go: GameObject = Instantiate(theObject);
       go.transform.position = theNewPos ;
   }
}
Posted
Updated 27-Sep-17 22:05pm
Comments
Richard Deeming 28-Sep-17 11:05am    
The code you've posted is not C#, nor is it JavaScript. So why have you tagged your question with those languages?

1 solution

Quote:
var theNewPos = new Vector3(Random.Range(minXPos, maxXPos), 0, Random.Range(minXPos, maxXPos));
It looks both x and z are random picked.

var theNewPos = new Vector3(Random.Range(minXPos, maxXPos), Random.Range(minYPos, maxYPos), Random.Range(minXPos, maxXPos));

Should do the trick (if you wish to maintain randomness on the z axis too)
 
Share this answer
 
Comments
CL4Y3R-TR 28-Sep-17 9:05am    
I tried, but it did not work
CL4Y3R-TR 28-Sep-17 10:01am    
Could you write this as c # code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900