Unity Random Game Objects are spawning out of bond

I suspect your background is centered at the origin (0;0). It means it spans from (-width/2;-height/2) on bottom-left corner to (width/2; height/2) on top-right.

Hence, if you generate values between (0;0) and (width;height), then only the top right corner of background is covered, and food can go beyond this.

Two solutions:

A) Move you background so that its bottom left corner is 0;0

B) Generate food location in the correct range, by simply subtracing (width/2) and (height/2) on respective values.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top