- Home /
Random position in rectangle, but not in a camera view
How can make a random coordinates generation, that are in rectangle AND not in a camera view?
Answer by whydoidoit · Jun 21, 2013 at 09:36 AM
So iteratively it would be easy, but to ensure constant runtime performance, how about something like this.
var screenRect = new Rect(20,20,400,400);
var cameraRect = new Rect(50,50,100,100);
var x = UnityEngine.Random.Range(screenRect.xMin, screenRect.xMax - cameraRect.width);
var y = UnityEngine.Random.Range(screenRect.yMin, screenRect.yMax - cameraRect.height);
x = x > cameraRect.xMin ? x + cameraRect.width : x;
y = y > cameraRect.yMin ? x + cameraRect.height : y;
Thanks, but in order to get this working, I need to get camera's view bounds in world coords. How can I get them? Quick example from random image: http://i.imgur.com/GWF55Nc.png
The view bounds in terms of what though? Pixels on the screen? Some kind of plane?
Yes, I guess pixels on the screen, like in that image. By the way, if camera won't be rotated exactly top-down, then view won't be a normal rectangle, right? So I think this code won't work. Basically what I want to do, is I have a rectangle area and I want to spawn enemy in that area, BUT not in view of camera, so player won't see how it will spawn.
So actually you want to spawn it on the plane on which it will walk right?
Your answer
Follow this Question
Related Questions
Getting other cameras coordinates 1 Answer
I want to put a random filter over the camera every few seconds. 0 Answers
Multiple Cars not working 1 Answer
Making a camera list 1 Answer
How to spawn GameObject exactly in front of another object? 3 Answers