- Home /
Moving gameobject relative to resolution
I am making a scrolling game-object, and it is supposed to warp to outside of the opposite border it leaves when it scrolls to the border. Right now, the position at which it transforms is hard-coded, but the problem with that is when I use a smaller resolution, the gameobject has to move "further" to transform. Is there anyway for me to do this, relative to the resolution? (e.g 1280x720, 800x600)
Also, if anyone knows a good C# resource for a person with C++ experience, it'd be appreciated. Thanks
Answer by aldonaletto · Mar 31, 2013 at 03:36 AM
You should find the screen borders in 3D space with Camera.main.ViewportToWorldPoint. That's an easy job when the camera is in Orthographic mode:
float leftX = Camera.main.ViewportToWorldPoint(new Vector3(0,0,0)).x;
float rightX = Camera.main.ViewportToWorldPoint(new Vector3(1,0,0)).x;
But if the camera is in Perspective mode, things get a lot more complicated: you should pass the distance between the object and the camera in Z to ViewportToWorldPoint, but you don't know at which distance the object would be when at the corners! A possible solution is to create a logic plane at the object position and perpendicular to the camera, create a ray with Camera.main.ViewportPointToRay, use plane.Raycast to find the distance of the hit point, then use ray.GetPoint to get the hit point - hope your camera is orthographic!
I fixed my code using that, thank you so much! (I used orthographic)
Your answer
Follow this Question
Related Questions
Transform position? 2 Answers
Teleport script, who can help me? 2 Answers
Making The Hole In A Golf Game 1 Answer