1
votes

I'm new on unity and am trying to create a top-down game.

My Main camera projection set to Orthographic.

I added a canvas with Render Mode of "Screen Space - Camera" and UI Scale Mode of "Scale With Screen Size" to hold my joystick and other UI element to support different resolutions. Also I added another canvas with the same settings to hold the walls and doors (Because they should be at the border or edge of screen along with different resolution!). I need to place some objects to the screen, I can do it by random with specific distance or by creating empty game objects on screen (inside canvas) and bind them to the script then place my Prefabs to their (empty GameObject) location.

The issue is I can't understand the object position in Canvas and Screen, I instantiate my prefabs to the empty gameobject position on Canvas, but it's been placed some where else.

Here's the 3d view of my scene:

my scene

1

1 Answers

1
votes

You could use Camera.main.screenToWorldPoint and use a random float or integer for the x and z coordinates. If you set the random number to be between the lower right corner in world space and the upper left corner in world space, you will get a random position inside the walls.