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).

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);
}

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))

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…

you can use 2 cameras one orthographic and other perspectve .orthographic for mouse input. Perspective for rendering

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!");
            }          
        }

    }
}

The one thing to note regarding @Tomer-Barkan answer is that when using it within Editor’s OnSceneGUI/duringSceneGUI/etc. on some screens the number of pixels per point may change and thus window’s 2d position may be not in pixels.

This is a version with changes regarding pixels per point, without which I experienced that final point wasn’t exactly where it should be:

public static Vector3 GetWorldPositionOnPlane(Vector2 screenPosition, float z)
{
	Ray ray = SceneView.currentDrawingSceneView.camera.ScreenPointToRay(
		new Vector3(
			screenPosition.x * EditorGUIUtility.pixelsPerPoint,
			SceneView.currentDrawingSceneView.camera.pixelHeight - screenPosition.y * EditorGUIUtility.pixelsPerPoint
		)
	);
	Plane xy = new Plane(Vector3.forward, new Vector3(0, 0, z));
	float distance;
	xy.Raycast(ray, out distance);
	return ray.GetPoint(distance);
}

z parameter would be current object’s z from transform.position.z

Awesome answer, thank you very much!