- Home /
Spawn Object in the sides of the screen
Hi. I'm trying to spawn gameobject and the X needs to be a random number BUT between the Screen left side and right side:
Vector3 final = Camera.main.ViewportToWorldPoint (new Vector3 (Random.value, 1, 10));
Instantiate(cube,final,Quaternion.Euler(0, 180, 0));
It works great but what if the game object spawns too left or too right? In that situation half of the object is visible and the other half you cant see it because its out of the range..
any suggestions?
bit late but try getting the size of the collider from collider.bounds http://docs.unity3d.com/ScriptReference/Bounds.html & add that to the current position or you can find the most left & right vertice in the mesh using http://docs.unity3d.com/ScriptReference/$$anonymous$$esh-vertices.html & a for loop then add that to your positon.
Answer by AndresBarrera · Jan 13, 2015 at 02:13 PM
Use Random.Range and define a "safe zone" between 0 and 1 (viewport coordinates)
float min = 0.2f;
float max = 0.8f;
Vector3 final = Camera.main.ViewportToWorldPoint(new Vector3(Random.Range(min, max), 1, 10));
Your answer
Follow this Question
Related Questions
Spawn object with different material. 0 Answers
Number of spawned objects 1 Answer
Place object in place of ghost. 0 Answers
How to Use Object Pooling for a 3D Autorunner? As Opposed to Instantiate? 0 Answers
2D runner, need a help 0 Answers