- Home /
camera.screentoworldpoint in Perspective
It works in Orthographic. Then I turn the projection to Perspective and it stops working, though the view is 100% the same. As I understand that`s because of nature of camera.screentoworldpoint.
How do I make it work in Perspective?
(I`m using camera.screentoworldpoint to move the object with cursor).
I used it in perspective and it works just fine. Can you explain why you think it doesn't work? Give examples, and even screenshots.
Sure.
here`s the tutorial http://www.youtube.com/watch?v=-mtzOvdhsuo
I have a Cube and a Camera. Cube is at origin. Camera: Orthographic, Size - 5, Far - 10.
The Code:
var myCamera : Camera;
function Start () {
}
function Update () {
var vec : Vector3 = myCamera.ScreenToWorldPoint(Input.mousePosition); vec.z = 0.0; transform.position = vec;
}
So I play and everything`s fine the Cube follows the Cursor. I switch to Perspective and it stands still ((
$$anonymous$$ake sure you set the z parameter to the camera near clip plane. If you just pass the raw screen coordinates (a Vector2) you will get the cameras position as Z (distance to project from camera) will be set to 0.
Answer by Tomer-Barkan · Oct 30, 2013 at 08:15 PM
When using a perspective camera, changing the Z position actually changes where the object appears on the screen. Think of it this way, in perspective camera, the farther the object is, the more it appears to be in the middle, even if the X and Y remain the same. The closer it gets, the more it moves to the side, until it gets close enough to pass to your left or right.
But fear not, there is a solution. You need to create a plane, on which you want to find the world position. In your case it seems you need the plane where z = 0. Then you convert the screen position to a ray, and find the place where that ray intersects with the plane:
public Vector3 GetWorldPositionOnPlane(Vector3 screenPosition, float z) {
Ray ray = Camera.main.ScreenPointToRay(screenPosition);
Plane xy = new Plane(Vector3.forward, new Vector3(0, 0, z));
float distance;
xy.Raycast(ray, out distance);
return ray.GetPoint(distance);
}
$$anonymous$$any thanks! Could you please suggest how should I adapt it for a top-down perspective?
It actually was intended for top down (where the objects X and Y change, but Z is always zero). I made a small mistake and fixed it (changed Vector3.up
to Vector3.forward
)
cool!
sorry, should I paste it as is or after my initial code?
It's a method. Paste it as is, and call it ins$$anonymous$$d of calling ScreenToWorldPoint
.
It's actually C# so you'll need to convert it to javascript
in top-down in my opinion x and z change and y is 0 :))
Answer by RodrigoAbreu · Feb 20, 2019 at 08:42 PM
Without using Raycast is still possible, however, if your camera is in perspective, you should remember that now the mousePosition conversion should also include z, as the mouse has actually no Z, it will try to use whatever values, and when you try to use ScreenToWorldPoint you'll get 0, 0, -10 ( if your camera z is on -10). With that said you should make sure that the depth is the same to convert from mousePosition to worldPoint also considering the camera's z for the conversion:
camera.ScreenToWorldPoint(new Vector3(-Input.mousePosition.x, -Input.mousePosition.y, camera.transform.position.z))
Answer by karma0413 · Oct 14, 2017 at 11:12 AM
I finally figured it out.... i was having a simiar problem....
Quick Description of my problem: I was using the Canvas to display a sprite ( a healthbar) which floats over all the characters. The problem however, was when i tried to obtain the WorldToScreenPoint it kept giving a result that was slightly off.... for example: the healthbar looked a little okay when the character was immediately in front of the camera... but as the character walks to the edge of the camera's fulstrum, the screens x,y placement becomes more and more incorrect.
Days and days of research and trying different combinations finally showed me that maybe there is a scaling issue which pointed me to look at Canvas / Canvas Scaler / scaling mode: scale with screen
Originally, this worked wonderfully when i had only 1 character and his healthbar stayed stuck to the top of the screen like old classic double dragon games. BUT when i made the decision to have many characters and they all need "floating healthbars", i didnt come back to re-evaluate whether this option needed to change.
Setting the canvas Scaler to: keep constant pixel size , fixes the problem and i now have the correct WORLDtoSCREENpoint that i needed! And now the healthbar floats beautifully above the characters...
BUT WAIT, ANOTHER PROBLEM! Now, if the screen resolution is small... the Ui sprite is obsurdly large..... and if the screen resolution is high definition then Ui sprite is way too small!
QUESTION: So how do i use the "scale with screen size" mode, but yet also still get back a correct WorldToScreenPoint?
ANSWER: you must take into consideration the overal scaling of the canvas when it is stretched to fit (whatever current resolution that you are using)
INSTEAD OF:
RectTransform myRect = GetComponent<RectTransform>();
Vector2 myPositionOnScreen = Camera.main.WorldToScreenPoint (myOwner);
myRect.anchoredPosition = myPositionOnScreen;
YOU CALCULATE THE OVERALL SCALE FACTOR LIKE THIS:
RectTransform myRect = GetComponent<RectTransform>();
Vector2 myPositionOnScreen = Camera.main.WorldToScreenPoint (myOwner);
Canvas copyOfMainCanvas = GameObject.Find ("Canvas").GetComponent <Canvas>();
float scaleFactor = copyOfMainCanvas.scaleFactor
Vector2 finalPosition = new Vector2 (myPositionOnScreen.x / scaleFactor , myPositionOnScreen.y / scaleFactor);
myRect.anchoredPosition = finalPosition;
If this helped anyone please log in to give me a thumbs up....
Hello, I am trying to use your solution in my game with orthographic cameras. When I use this solution, the health bars jitters around whenever the camera moves. At first, I thought this was a problem in the Unity editor because I thought that if I reloaded the scene, or loaded into it from my main menu, then it wouldn't jitter, but now it looks like it is always jittering. Any idea how to fix this? Thanks.
Answer by Waterblade · Apr 05, 2021 at 06:29 AM
you can use 2 cameras one orthographic and other perspectve .orthographic for mouse input. Perspective for rendering
Answer by HuldaGnodima · Dec 04, 2021 at 05:00 PM
Whenever I google "perspective camere unity get mousepos" I always end up here, but the solutions don't work for me. I'm using a 2D-world and 2D-colliders + a perspective camera. So for anyone with a similar setup here is my solution, maybe it will work for you:
public class PlayerInputHandler : MonoBehaviour
{
public Camera mainCam;
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
//Below the mouseposition will be calculated in the world-position
var mousePos = Input.mousePosition;
mousePos.z = -mainCam.transform.position.z;
Vector3 mousePosInWorld = mainCam.ScreenToWorldPoint(mousePos);
//Here a check is made to see if 2D-colliders overlap some 2D-colliders
Collider2D allColls = Physics2D.OverlapPoint(mousePosInWorld);
if (allColls != null)
{
Debug.Log("Hit a 2D-collider!");
}
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613120943im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Please explain Quaternions! 7 Answers
sound registry 0 Answers
rigidbody.AddForce() - I just don't understand it. 1 Answer
Multitouch iPhone error! Help 1 Answer