Search Unity

Bug Pixel Perfect Camera and ScreenToWorldPoint Bug?

Discussion in '2D' started by Zodiak, Oct 13, 2021.

  1. Zodiak

    Zodiak

    Joined:
    May 10, 2013
    Posts:
    13
    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?
     
    Last edited: Oct 13, 2021
  2. kennyy_

    kennyy_

    Unity Technologies

    Joined:
    Apr 7, 2021
    Posts:
    96
    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.
     
  3. Zodiak

    Zodiak

    Joined:
    May 10, 2013
    Posts:
    13
    @kennyy_ , hello!

    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.


    Vector3 mousePosition = _playerInput.actions["MousePosition"].ReadValue<Vector2>();
    mousePosition.z += 10f;
    Vector3 worldPosition = Camera.main..ScreenToWorldPoint(mousePosition);


    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.
     
    Last edited: Oct 15, 2021
  4. kennyy_

    kennyy_

    Unity Technologies

    Joined:
    Apr 7, 2021
    Posts:
    96
    I've tested using 2021.1.12f on both 2D Pixel Perfect Package 5.0.1 and URP Pixel Perfect 11.0.0. Both of which are working as intended.

    ezgif-1-fe5a6e3ac7ad.gif

    Try using the following code snippet
    Code (CSharp):
    1. Vector3 mousePos = Input.mousePosition;
    2. mousePos.z += 10;
    3. Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(mousePos);
    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.
     
  5. Zodiak

    Zodiak

    Joined:
    May 10, 2013
    Posts:
    13
    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?
     
    Last edited: Oct 21, 2021
  6. Blake067

    Blake067

    Joined:
    Nov 9, 2017
    Posts:
    4
    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.
    Code (CSharp):
    1.  
    2.         public Vector2 CorrectByRect(Camera camera, Vector2 pointer)
    3.         {
    4.             var size = camera.rect.size;
    5.             var offset = camera.pixelRect.position;
    6.             return new Vector2(pointer.x * size.x + offset.x, pointer.y * size.y + offset.y);
    7.         }
     
    Last edited: Oct 23, 2021
  7. Racso_JC

    Racso_JC

    Joined:
    May 18, 2019
    Posts:
    3
    Is there a fix planned for this?

    Blake067 literally pointed out the exact error, which I encountered today (and which, obviously, made me waste an insane amount of time).
     
  8. erdostamasa

    erdostamasa

    Joined:
    Mar 28, 2020
    Posts:
    32
    Any solutions to this?
     
  9. jordanflammia

    jordanflammia

    Joined:
    Dec 12, 2021
    Posts:
    3
    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
     
    erdostamasa likes this.
  10. bluefairymediallc

    bluefairymediallc

    Joined:
    Dec 10, 2019
    Posts:
    10
    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.