- Home /
Camera.ScreenToWorldPoint Not Giving Right Point
So I have to following lines of code and all I'm trying to do is set tempTiles position to the bottom left hand corner of the screen. However, with these lines of code the position is not 0,0,0 but rather -8, -4, 0. Could someone please tell me why this is happening? Also if it's worth mentioning I'm working in 2D.
Vector3 screenPoint = new Vector3(0,0,0);
Vector3 worldPos = Camera.main.ScreenToWorldPoint (screenPoint );
Quaternion ZeroRotation = new Quaternion(0,0,0,0);
GameObject tempTile = (GameObject)Instantiate(Resources.Load("Tile"),worldPos,ZeroRotation);
Answer by Aram-Azhari · Nov 14, 2013 at 07:26 AM
If you want the object to be at the position 0,0,0 , just write:
Vector3 worldPos = new Vector3(0,0,0)
But if you want the object to be at the bottom left, what you do is correct, but don't expect it to be 0,0,0 in the world. Your comparison is wrong.
In order to check that you have set the right position, add these lines after the gameobject creation:
Vector3 screenPos = Camera.main.WorldToScreenPoint(tempTile.transform.position);
Debug.Log(screenPos);
As you can see, it is still 0,0,0 which means you have correctly set the place of the object at the corner of the screen.
If all I wanted to do was set an objects position in screen coordinates could you tell me how I would do that?
Actually what you're doing is correct. I tested your code and if you look at the editor, it places it on the corner. However, since you are using Camera's worldtoscreenpoint, your object is placed just at the camera's z level. What I mean is, when the worldPos is (0,0,0) the object will also be at (something,something, 0).
What you should do is to change the line to:
Vector3 worldPos = new Vector3(0,0,10)
or any other number so the object will be in front of the camera.
So, if I understand correctly my object actually is being drawn at the bottom left hand corner but I just can't see it as it's being drawn on the same z of the camera?
Alright thank you very much, I'll have to try and change that when I get a chance.
Answer by DanPHernbrott · Nov 14, 2013 at 07:17 AM
Specifically, the SCREEN position is (-8, -4, 0)? Or the world position? Because the world position could potentially be anything, depending on the camera location.
Answer by Jix · Nov 14, 2013 at 08:17 PM
This is not the right way to do it.
There are 2 ports to deal with the camera, screen bottom left corner is not (0,0), it has different value, a negative one... what you need is ViewPort not screenPoint. you shoud do this instead
Vector3 screenPoint = new Vector3(0,0,0);
Camera.main.ViewportToWorldPoint(screenPoint );
Well technically this isn't the wrong way, but when you have only one camera where the view port is exactly the same as your screen, this won't be necessary. But of course, in a case you have a small map of your RTS game where that map is another camera, this makes sense.
What are the odds that his bottom left of his screen will be exactly at (0,0,0)?
It isn't something you'd estimate, it is something you can calculate. It is always 0,0,0. Unless you post a code that runs otherwise. But hey, if viewport does it without any odds, then users should try that.
Your answer
Follow this Question
Related Questions
Lock the screen 1 Answer
Camera Shake 4 Answers
Clicking Trigger 1 Answer
Orthographic cameras and screen resolutions 1 Answer
Camera setup question 2 Answers