s3e3
s3e3

Reputation: 131

Can someone explain Unity co-ordinate system in 2D to me please?

I'm trying to make a 2D game. I have 4 gameobjects which i want to place in each corner of the screen., i.e when I run my app on my phone it should be visible in each corner. So what I did in the script is,

//GameObject 1 script
void Start(){
    transform.position = Camera.main.ScreenToWorldPoint(new Vector2(Screen.width, Screen.height));
}

//GameObject 2 script
void Start(){
    transform.position = Camera.main.ScreenToWorldPoint(new Vector2(0, 0));
}

//And same for other 2 gameobjects...

But i am not seeing any of the objects on my screen.

Upvotes: 0

Views: 57

Answers (1)

vmchar
vmchar

Reputation: 1323

You're doing it in right way, but there could be some moments you should check out:

  1. Make sure that you're using correct camera to translate coordinates from screen to world.]
  2. Make sure that z position of your object are inside camera's clipping plane (it must be smth between camera's near clipping plane and camera's far clipping plane). You can start you game in editor and look at scene view if gameobjects are actually inside the clipping plane.
  3. What you're doing is placing your gameobjects so that the center of each gameobject will be in the corner of the screen. And it's not like gameobject will fit the corner. If you want to place gameobject so that they will fit the corner (not their center will be placed in the corner) you should calculate the offset for each gameobject. You can user renderer.bound for this purpose.
  4. Make sure that your camera renders the layer on which gameobjects are. You can check it in camera's culling mask.

Upvotes: 1

Related Questions