- Home /
Middle of screen vector 3?
There's a line in my script which says "transform.Translate (0.31,0.21,0); }" the vector 3 I believe is called is as close as I can get to having something in the middle of my screen, but I need it perfectly in the middle can anyone post the actual numbers?
Answer by dondiego · Oct 30, 2011 at 12:54 PM
Use Camera.ViewportToWorldPoint.
function Update () {
var cam = camera.main;
var v = cam.ViewportToWorldPoint(Vector3(0.5, 0.5, cam.nearClipPlane));
transform.position = v;
}
Attach this script to the object you want to have in the middle of the screen.
Answer by syclamoth · Oct 30, 2011 at 12:53 PM
It is impossible for any specific 'number' in world space to correspond to the exact middle of the screen- it always depends on the size of the screen, the position of the camera, and the location of the object itself!
Besides that, because a screen transforms a three-dimensional space into two dimensions, any single point on the surface of the screen will naturally correspond to an infinite ray projecting out into your game world.
Which brings me to my next point- Camera components have this wonderful functionality called
Camera.ViewportPointToRay(Vector3);
in which you give it a point on the screen, and it returns a ray shooting off into your world!
If you know how far away from the screen you want your object to be, you can find that position by using
correctPos = Camera.main.ViewportPointToRay(new Vector3(0.5F, 0.5F, 0)).GetPoint(distance);
Your answer
Follow this Question
Related Questions
I want to turn around an object 2 Answers
C# GameObjectList not Setting Parent 0 Answers
C# Transform Issues 1 Answer
What is wrong with my zipline script? 2 Answers
C# The call is ambiguous between the following methods or properties 1 Answer