Placing random game objects on canvas in random position with specific distance - unity3d

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:

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.

Related

Rotating second object to match rotation of first object relative to camera in Unity

I am attempting to make an object that is attached to the UI achieve the same appearance/rotation relative to the camera as an object that is being looked at in world space. For example in the image, how would I get the cube in the black UI which is fixed to the camera to look the same as the cube in world space no matter what angle I viewed the world space cube from/How would I get the UI cube to change its rotation to match the world space cube based on what angle I was viewing the world space cube from in real time?
I'm not wanting another camera view as a solution as I would like to change the object in the UI to other objects.

How do I get the 2d sprite gameObject in front of the UI Canvas?

hierarchy
2d sprite
panel UI
in game
Fist make sure your canvas' Render Mode is not set to Screen Space - Overlay.
If it is, then switch it to Screen Space - Camera and then change its Render Camera property to point to your main camera.
You can then use the following canvas' properties to control where it appears in relation to other sprites (which appears on top of which), the same way you control any other 2D Sprite:
Sorting Layer
Order in Layer
Also worth noting that 2D Sprites with same Sorting Layer and Order in Layer will use the distance to the camera to decide which one appears on top of which, and the same is true for the canvas:
If your canvas' Render Mode is set to Screen Space - Camera, it will always be positioned in front of your camera and you need to use its Plane Distance property in order to position it correctly.
If your canvas' Render Mode is set to World Space, you can position it freely in the world in the same way you position any other sprite.
First of all the hierarchy window in unity is completely irrelevant when its about positioning.
In Unity everything is 3D, even if you switch to 2D perspective.
So in your case you still have your camera, your sprite and your canvas in a 3D Dimension.
To set your sprite in front of the canvas you only need to increase the positioning stats (seen in your "panel UI" screenshot) at least slightly closer to your camera than the canvas is.

Offsetting the rendered result of a camera in Unity

I am trying to make everything that is rendered by my perspective "Camera A" appear 100 points higher. This is due to the fact that my App has an interface with an open space on the upper part.
My app uses face detection to simulate the face movement into an in game avatar. To do this I compute the "Model-View-Matrix" to set it into the camera's "worldToCameraMatrix".
So far this works well, but everything is rendered with the center as the origin, now i want to move this center origin a certain distance "up" so that it matches my interface.
Is there a way to tell Unity to offset the rendered camera result?
An alternative I thought about is to render into a texture, then I can just move the texture itself, but I thought there must be an easier way.
By the way, my main camera is orthographic, and i use this one to render the camera texture. In this case simply moving the rendering game object quad up does the trick.
I found a property called "pixelRect", the description says:
Where on the screen is the camera rendered in pixel coordinates.
However moving the center up seems to scale down my objects.
You can set the viewport rect/orthosize so that its offset or you can render to a render texture and render that as a overlay with a offset or diffirence in scale.
Cheers

Unity coordinates doesn't work

I'm very beginner in Unity so please forgive if, this questions isn't so hard to answer:)
So, I have a text on a Canvas in the editor, it is okay, it's showing well on Scene editor and In Game as well.
But, when I added two Sprites, which going to be the player and the enemy, the positions of these sprites are behave a bit weird.
The text position is: x: -293 y: 195, when I'm modifying the position of the text it works fine.
When I add the sprites to x:0 y:0 and x:1 y:1, in the scene editor they appear in the left bottom corner, but when I check in the game, they placed in the middle of the screen.
My question is why the coordinates and the positions are so different on Scene (grey) and on Game (blue) ?
Because initialized render mode of Canvas in Unity is "ScreenSpace - Overlay". So it is shown on too big area in scene. If you want to work only in view field of camera, in inspector just change render mode of Canvas to "ScreenSpace-Camera" and drag your MainCamera to RenderCamera in inspector. Even if you use ScreenSpace-Camera, coordinate system of RectTransform (UI Objects transform) is different than Transform (Normal game objects transform)
in this view, if you get closer to the left-down corner of your scene, you will see your main camera area and Sprites that are in correct positions.
I hope this helps.

Vuforia how to place persistent models in the world

I am using Vuforia and Unity to do AR. So far I have gone through the ImageTarget example, and am able to see the teapot using my Samsung Galaxy S4. I've also managed to overlay some UI elements on the screen.
Now, I'm trying to place a persistent 3d object (capsule for example) in the world.
However, when I place a capsule say, next to the teapot, I see only a white screen when I start the android app.
Why is this so? How can I resolve this?
My scene hierachy is like this
ARCamera
Directional Light
ImageTargetChips
ImageTargetStones
Canvas <-- for UI overlay
EventSystem
Capsule <-- adding this into the world causes screen to turn white
Even if I shift Capsule to under ARCamera, I only see white when I start up the app.
Thanks.
You have to learn about the Unity hierarchy. Everything, i.e., 3D objects, image, audio, etc., i.e content associated to specific image target must be Hierarchically child of ImageTarget.
Here, you need to put your 3DObject (Capsule) inside the image target (Stones/Chips).
Like
> +ARCamera
>
> -ImageTargetChips
>
> Teapot
> Capsule
And then adjust the position & Scale of capsule (3D Object)
Your 3D object must be in a camera view:
eg. Position of camera: x:0 y:0 z:0
Position of image target: x:0 y:0 z:-60
Here, Z axis is "Depth"