Camera Location

I am in the process of moving over to unity from MonoGame / XNA. The one thing that has me baffled however is locations on the screen. I am sure many new people to Unity run into this problem as well. To be more specific I have an application that allows for dynamic content to be loaded and displayed on the screen. With XNA the scope was broad (or so it seemed), as if I wanted an object to be placed at let us say X 1022 Y 480 I would just say this:

// shorthand code
object.position.x = 1022
object.position.y = 480

However, in unity when I do that the object I noticed goes off the screen to an very (and I mean very) far away position not in view of the camera whatsoever. I am pretty sure my major hang up here is the camera. I have read several documents pertaining to the camera and how it works; but for some reason it is not clicking.

I am currently using an Orthographic type camera with a Size of 20, Clipping panes (N0.3 F1000) Rect (X0 Y0 W1 H1) as this allows me to display a 1920x1080 background image with a Z of > 10 and have it cover the entire camera space.

I guess what I am saying in short is in the theme files (my application uses theme files that loads custom images (sprites) and effects) how do i lets say position an object in the following manner:

Object 1: 10 pixels from bottom left and 10 pixels from bottom
Object 2: 20 pixels from top right and 100 pixels from top

The above are generic lines of code but they plainly state what I am trying to achieve here with this new system. I am sure the answer is as clear as day and staring me right in the face but for reasons unknown it has eluded me. Any help on the topic would be greatly appreciated and welcomed whole heatedly.

Good day.

1 Answer

1

Sounds like you need

When you say transform.position.x = 300, it means 300 on the x axis in world coordinates, not in screen coordinates. So, you have to first transform the screen pixel value into a world value. The only thing that could by tricky is how to determine the z position which , in Camera.ScreenToWorldPoint, will mean the distance from the camera forward into worldspace. One way to do it is to use a Raycast starting from the center of the screen going towards the camera’s forward vector, and then taking the point that was hit by the ray (probably the ground or something). The Vector3.Distance between the camera and the hit.point would then be the z value.