- Home /
Question by
Lionlev · Mar 02, 2017 at 12:40 PM ·
generationscreentoworldpoint
Create prefab within the screen? (2D game dev)
Hi, I have a simple loop that creates instances of my prefab texture. It works however I need it to consider the size of the texture as well. So no textures would be generated on the edge of the screen. Whole texture must be inside of the screen.
The current code example is:
for (int i = 0; i < numberOfDupes; i++)
{
Vector3 screenPosition = Camera.main.ScreenToWorldPoint(new Vector3(Random.Range(0, Screen.width), Random.Range(0, Screen.height), Camera.main.farClipPlane / 2));
Instantiate(prefab, screenPosition, Quaternion.identity);
}
Thanks for the help!
Comment
Answer by tinglers · Mar 03, 2017 at 09:29 PM
if you know the size of the texture you can subtract it from the random range to make: Random.Range(0. Screen.Height - texture.height)
, making sure the texture in its entirety is within the height.
Your answer
![](https://koobas.hobune.stream/wayback/20220612112310im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
how one would go about using Triangulator in javascript? 0 Answers
Procedural terrain generation ? 0 Answers
Dyanmic Platforms and Erasing Geometry? 0 Answers
Randomly Generated map 0 Answers