- Home /
Random instantiate position doesn't work
I'm using Orthello to create a 2D game, and I'm trying to instantiate some game objects at random positions, but the objects always show up in the middle of the screen. I've debugged the variables, and the random position is getting generated. I just don't know why Unity aren't using it.
I appreciate any help.
Here is my code:
var abobora: Transform;
var banana: Transform;
var bolinho: Transform;
var copo: Transform;
var garrafa: Transform;
var garrafa2: Transform;
var garrafa3: Transform;
var hotdog: Transform;
var jornal: Transform;
var maca: Transform;
var milho: Transform;
var morango: Transform;
var pao: Transform;
var papel: Transform;
var pato: Transform;
var sorvete: Transform;
var lixo: Array;
function Start() {
lixo = [
abobora,
banana,
bolinho,
copo,
garrafa,
garrafa,
garrafa,
hotdog,
jornal,
maca,
milho,
morango,
pao,
papel,
pato,
sorvete
];
InvokeRepeating('CriaLixo', 3.0, 5.0);
}
function CriaLixo() {
var indice_aleatorio = Random.Range(0, 15);
var posicao_aleatoria: Vector3 = Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), 0);
var rotacao_aleatoria = Quaternion.AngleAxis(Random.Range(-180.0f, 180.0f), transform.right);
var objeto_aleatorio = lixo[indice_aleatorio];
var game_object = Instantiate(objeto_aleatorio, posicao_aleatoria, rotacao_aleatoria);
}
Answer by supercouge · Jul 19, 2013 at 10:23 PM
...
var posicao_aleatoria: Vector3 = Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), 0);
// posicao_aleatoria may be, for example : x : 600, y = 800, z = 0;
var game_object = Instantiate(objeto_aleatorio, posicao_aleatoria, rotacao_aleatoria);
// game_object is now at a position of x = 600, y = 800, z = 0. So, of course, there is little chance that you can see it. You should have used Camera.ScreenToWorldPoint(). For example:
var posicao_aleatoria: Vector3 = Camera.current.ScreenToWorldPoint(Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), Camera.current.nearClipPlane));
Edit : What I mean is, you used a position on your screen (a pixel value) to place an object in your 3D world (in meters). ScreenToWorldPoint() allow you to transform the coordinate on your screen to the coordinate on your world.
Edit2 : Sorry, I forgot to set a correct value for "Z". According to the documentation, when you use ScreenToWorldPoint(), the z position is in world units from the camera. So, with Z = Camera.main.currentClipPlane(), all your objects appear at 0.3m of your Camera. To correct it, you have to set a another value for Z. It's up to you on how you want to implement it, but here is a example of solution:
Vector3 screenPos = new Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), Vector3.Distance (ts[idx].position, Camera.main.transform.position));
Vector3 pos = Camera.main.ScreenToWorldPoint(screenPos);
// Thanks to Vector3.Distance(), your "clone object" will at the same distance from the camera than your "original object".
With that, your object shouldn't be close to the Camera anymore, and therefore will not appear at the center of the screen.
Thanks for your answer, but unfortunately all objects still being created at the center of the screen. Debug.Log show that random positions are generated each loop, but Unity ignores it ¬¬ $$anonymous$$aybe it's something from Orthello...