So I’ve been building a game with Pixel Perfect Camera and was attempting to use ScreenToWorldPoint but I think the distortion that happens from the component make ScreenToWorldPoint return distorted data.
Keep in mind the data I’m about to show is (Mouse Position) - (World Position)
With Pixel Perfect Camera (Experimental) off, clicking the top right of the screen returns.
(1237.8, 691.8, 0.0) - (29.3, 25.2, 0.0)
With Pixel Perfect Camera (Experimental) on, clicking again the top right of the screen returns.
(1240.4, 694.5, 0.0) - (25.2, 22.9, 0.0)
Heres a gif of it so visually you can see something is screwy…
So I guess my question is this something that is just known and I have to find another sollution or is there a simple fix for this that I haven’t come across? Bug?
Hi @Zodiak , can you post the script code for more clarity? What version of unity are you using and is this Pixel Perfect camera from the 2D Pixel Perfect Package or URP?
I’ve tested locally with the URP Pixel Perfect and ScreenToWorldPoint is working correctly.
Version : V2021.1.12f1 Personal
2d-pixel-perfect Version: 5.0.1
URP Version: 11.0.0
Code is nothing special. Get "MousePosition" from my player input. (I’ve also tried used Mouse.current.position with the same results) offset the mouse position based on how far the camera is from the plane, which is 10. My confusion with is that without a code change with the component off it would work fine, but with it on it would work wonky (which you’ve seen) The Pixel Perfect Component I think is from the package, its the one tagged as experimental as its the only one that works with the URP I believe. Oh and I snipped out, but after this bit of code below I just instantiate the image on the worldPosition, didn’t think its super relevant.
My current fix is I use a non-rendering camera that doesnt have a pixel perfect component on it literally just for the ScreenToWorldPoint. The above code I changed from my nonRendering camera to the original Camera.main code.
Another thing you can check is that there is only one MainCamera tagged camera active in the scene. This will be camera used to calculate the screen to world point.
I apologize that I haven’t been able to respond… been working, but the most notable issue I had was towards the edges of the screen (as showcased on my gif) have you for sure tested that? I see you clicking generally in the middle of your screen.
The main camera issue you mentioned isn’t a thing because I have only one camera ever instantiated.
In regards to the code you provided, that’s using the old input system correct? I’d have to swap my whole project just to test it?
I encountered same issue.
The pixel perfect camera’s “Crop Frame” was turned on, which caused the camera’s “ViewPort Rect” be modified.
So you can’t use “Camera.main.ScreenToWorldPoint” directly.
public Vector2 CorrectByRect(Camera camera, Vector2 pointer)
{
var size = camera.rect.size;
var offset = camera.pixelRect.position;
return new Vector2(pointer.x * size.x + offset.x, pointer.y * size.y + offset.y);
}
Yeah I would love for this to be addressed. I have a large project that is affected by this issue. Currently doing a lot of hacking apart of the urp to make things work. Not what I would consider ideal. I know unity is more 3d centric but let’s show some 2D love
I’d also very much love to see a fix for this. OnMouseOver and OnMouseExit don’t function properly due to the same issue, so even simple things like hovering over a sprite is broken.