Search Unity

Physics2DRaycaster and camera

Discussion in 'UGUI & TextMesh Pro' started by drHogan, Dec 18, 2014.

  1. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Hi guys,

    I have a weird behavior with the Physics2DRaycaster attached to my main camera. My objects in the scene have a 2D collider attached and a script implementing IPointerClickHandler.

    When the camera (ortographic, planes all correct so that the are fits in) is zoomed out (size 340 and a bit less) as in this first picture. The tavern (small building between trees on upper left) and whatever other building i decide to build in that upper part don't react to the raycaster and i always receive the message that i am hitting the terrain on click.


    At the same level of zoom, if they are anywhere else in the map they cause no problem. When I zoom in (just changing the size of the camera and the x,y position in case, but not the distance from the scene) then, as in this picture here, i see that the area of "non raycasting" shrinks more and more until almost disappearing.



    I am going stupid in trying to figure out the problem, any idea?

    Best,
    H

    Might also be worth pointing out that eveything else works in that area, so if for example i want to build a tower there, i have no problem in building it, just i can't select it after!
     
  2. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    I further tested, and even by checking from the inspector the data coming to the even system, when it's zoomed out it doesn't see the tavern or other stuff at all, always returns only the terrain behind.

    But what's even weirder is that this code, at the same time instead sees the collider of the tavern properly doesn't matter the level of zoom and the position.

    Code (CSharp):
    1. if(Input.GetMouseButtonDown(0)){
    2.             RaycastHit2D hitP = Physics2D.Raycast(new Vector2(mainCamera.ScreenToWorldPoint(Input.mousePosition).x, mainCamera.ScreenToWorldPoint(Input.mousePosition).y),Vector2.zero,0.0f);
    3. //
    4.             if(hitP.collider!=null){
    5.                 Debug.Log(hitP.collider.name);
    6.             }
    7.         }
    I really begin to feel helpless on how to solve this.
     
  3. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Anybody any idea? I am still completely stuck with this problem, i really can't make any sense out of it doesn't matter what i tried
     
  4. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    i'm just curious but are the mainCamera.ScreenToWorldPoint(Input.mousePosition) the same position as what the EventSystem thinks the mouse position is? (debug log that value out / select the EventSystem GO and look at the inspector for the information).

    Make sure that your tavern is set to a layer that the Physics 2D Raycaster can hit. (i.e. the Event Mask)

    If they are the same any change the camera isn't the same (i know you've said you attached it to the main camera but just a thought).

    If that doesn't work, try pulling the source code (https://bitbucket.org/Unity-Technologies/ui/overview) and play around with modifying the Physics2DRaycaster. Who knows maybe doing GetRayIntersectionAll doesn't work perfectly or something. I'd debug.log the "hits" list to see if the tavern object is even part of it or not. Try changing the call to be the RaycastAll instead.

    if you object is part of the list but it still seems to be the one not getting the call then try pulling my new eventSystem ordering repo (https://bitbucket.org/philip_cosgrave/philunity-uibugfixes/branch/eventsystemrewrite) and see if that fixes it. It might have just been a ordering logic issue.

    I hate putting that sort of debugging on you the user but since its almost christmas i want you to have something else to look into.

    If all of that fails PM me with your project and i can take a look monday after the holidays.
     
  5. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Hi Phil,

    I will try what you suggested tomorrow after the gargantuan xmas lunch and see what I get out of it.

    On a very quick thought, what makes me wonder at all, is the fact that the raycaster made in the old way works perfectly, and when i get the tavern out of that upper area or i zoom in, also the targeting of the rays seems to work perfectly as well. But with that in mind I will try debugging further and keep you updated on what i get out of it.

    Thanks for answering, and have a nice time for holidays!

    Cheers,
    H
     
  6. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Yea it very well might be the fact the physics2dRaycaster uses GetRayIntersectionAll instead of RaycastAll. i havn't dug into the differences between these two function but something might be different.
     
  7. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    The first test gives me different values in the camera.main.screentoworld and the event system debug.that happens both in the problematic area and where it works correctly as well.

    H
     
  8. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Hi Phil,

    I couldn't pull the rep yet cause with the usb stick up in the mountains my download speed is awfully slow.

    But I played a bit around with the various raycasting anyway (in the mouse function where before i was getting the hit). I changed it this way :

    This one works perfectly with RaycastAll (returns both the tower and the terrain also when they are in the problematic area, and the list is sorted correctly it seems).

    this one has no effect at all, doesn't matter in which part of the screen i shoot it, i never obtain any single hit on anything

    Now I don't know if there are other more subtle details about their differences, but so far it seems to me that the Physics2D Raycaster should be probably using the RaycastAll if you need a list, or Raycast if you just need a hit.

    If i manage to pull the repo before heading for the north pole and disappearing from a week from the web, i will let you know

    Best,
    H
     
  9. phil-Unity

    phil-Unity

    Unity UI Lead Developer

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Hmm thanks for that, i might need to make that change then.
     
  10. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Hi guys,

    May I ask if the raycasting problem is into some todo list? I was dealing with other parts of the software, now I updated to unity 5, but the problem remained identical. Otherwise I had to revert to my older raycasting, but that was messier in structure and code, and since then we worked so much on the software that getting it all back in might become a nightmare.

    Best,
    H
     
  11. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    I know I am a bit annoying, but this raycasting problem is messing up a bit our alpha deadlines, may I ask if we can expect a solution about it soon?

    I noticed that it also happens in the center of the map with quite small sprites (and with those small ones, the older method still finds them easily). Once I zoom in the problem disappears
     
  12. RichCodes

    RichCodes

    Joined:
    Apr 3, 2013
    Posts:
    142
    I'm currently running into this. There still is no solution?

    *Edit, I mean besides the working kludge I use now.
     
    Last edited: Apr 8, 2016
  13. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Hi Rich, actually I was also about to revive this post. I took care for almost one year of other parts of the game, hoping that new versions of unity would solve it, but as of now, the test i made a couple of days ago showed me that the problem is still there.

    what workaround do you use?

    Cheers,
    Emiliano
     
  14. RichCodes

    RichCodes

    Joined:
    Apr 3, 2013
    Posts:
    142
    All of my interactable objects implement IPointerClickHandler.

    On my terrain I added a simple class that just implements IPointerClickHandler.

    Then inside of its OnPointerClick I perform a raycast.

    Code (CSharp):
    1.     public void OnPointerClick(PointerEventData eventData)
    2.     {
    3.         //Raycast to make sure nothing was missed.
    4.         RaycastHit2D hit = Physics2D.Raycast(inputCamera.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    5.         if (hit.collider && hit.collider.gameObject != this.gameObject)
    6.         {
    7.             IPointerClickHandler clickHandler = hit.collider.gameObject.GetComponent<IPointerClickHandler>();
    8.             if (clickHandler != null)
    9.             {
    10.                 clickHandler.OnPointerClick(eventData);
    11.                 return;
    12.             }
    13.         }
    14.     }
    What this means is extra raycasts I shouldn't have to be doing, but at least it works. Since only about 1/4 of the screen is getting the screwed up raycasts this really isn't horrible on performance. I just hate writing kludges in general :rolleyes:

    Interesting looking game by the way.
     
    Last edited: Apr 10, 2016
  15. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Mhm ok, makes sense as a workaround, but i noticed that I have the same problem also with very small (but still large enough to be hit IMO) sprites that are in the middle of the screen (I have resource buildings that can be build anywhere), so that still means that i can have problems all over the screen.

    definitely if the additional raycaster is only on the mouse click it will probably not screw up performances too much, but i would have expected this problem to be solved by now in unity.

    Thanks, lots of stuff in the screenshots above is 1 yrs old and temporary though, so it's evolving quite past that i would say.

    Cheers
     
  16. Peez-Machine

    Peez-Machine

    Joined:
    Jul 30, 2013
    Posts:
    27
    What version of Unity are you using? If your problem is what I think it is, the bug is fixed in a recent version of Unity, and was caused by the Physics2DRaycaster using Raycast instead of GetRayIntersection.

    https://fogbugz.unity3d.com/default.asp?725973_gq4bcqnt11hrn3p0

    As of right now, though, GraphicRaycaster is still broken in the same way, so you'll need to subclass GraphicRaycaster if using a world-space canvas.
     
  17. RichCodes

    RichCodes

    Joined:
    Apr 3, 2013
    Posts:
    142
    @Peez Machine

    I am using 5.3.4f1.

    I am a bit confused since for me Raycast works fine, GetRayIntersection does not!

    See drHogan's testing above, it comes to the same conclusion, and to top it off we have @phil-Unity saying on Dec 24th, 2014 that physics2dRaycaster uses GetRayInstersectionAll.
     
  18. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Using 5.3.2f1 here, the problem is still there indeed.
     
  19. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Ok, was checking now @Peez Machine link and indeed in the last reply in that page mentions that the bug has been fixed with version 5.3.4f1, but do you still experience it @RichCodes ? ( I have two projects with 2 different older unity versions running and i am afraid as usual to update before they are delivered).
     
  20. RichCodes

    RichCodes

    Joined:
    Apr 3, 2013
    Posts:
    142
    5.3.4f1 is actually the first time I noticed the problem. It's also the first game I've done using the event system where the objects are smaller.
     
  21. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Mhm ok, then I guess it has not been fixed indeed. It's kinda weird not many people have that problem, i wouldn't expect it to be that uncommon, and morover i've never seen a bug surviving for 2 years across so many unity versions, i hope they will fix it soon cause adding the workaround before release would not be the ideal thing for me to do.
     
  22. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376


    does the raycast hit if you do a second one, perpendicular from where the 1st one hit the ground?

    just wondering. :)
     
  23. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Hi @IzzySoft I am not completely sure if I understood the experiment that you propose. The current one is being used from the Physics2DRaycaster and therefore originates "automatically" from the camera, how would you mean to handle the second one? (also the scene is in 2d, so basically the raycasters supposedly always hit perpendiculare to the ground)
     
  24. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    Sorry, was spacing out. looked at the picture and imagined it was isometric. ;D
     
  25. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    No problem man ;) Well, simulating a sort of isometric-like view in a flat 2d was our plan, so the bug is still there but i achieved a little success anyway :p
     
    IzzySoft likes this.
  26. IzzySoft

    IzzySoft

    Joined:
    Feb 11, 2013
    Posts:
    376
    i have not used 2DRaycaster, but a few of the times when Physics.Raycast was used for kissUI, the box collider on the Background Image was at the same Depth as the Button Image i was trying to click on, but the background kept registering the hit... even though the button Image was clearly draw above the background image. :(

    They were both sharing the same Depth (z) position. :/
     
  27. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    Mhm no here it shouldn't be the case, my code already makes sure that all the sprites never superpose on the depth axis.
     
    IzzySoft likes this.
  28. the_lemur

    the_lemur

    Joined:
    Apr 3, 2013
    Posts:
    104
    I am currently having this same problem. The raycast works as expected for most portions of the camera, but the upper-left portion always gets the base tileMap instead of the sprites on top of it. It really is bizarre.
     
  29. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    I gave up on trying to find a solution and I am waiting for a solution (i have anyway a lot of work in the game), but so far nothing at all. It seems to have to do with both the position (upper portion) and the camera size. It has been 2 years already, and the issue is still there, no idea if any solution is currently planned, i would hope so, @phil-Unity anything?
    Cheers!
     
  30. the_lemur

    the_lemur

    Joined:
    Apr 3, 2013
    Posts:
    104
    Generally the cut-off point appears to be halfway up the screen. I'm willing to bet somewhere in the code, extents is being used instead of size. It's a waste of time but maybe I'll look at the repository.
     
  31. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    hi @the_lemur is it also in your case related to the zoom (camera size)? cause in mine, if i zoom in and get closer, i can also select the upper part of the screen more easily again. If I am zoomed out instead, i get a hit on the terrain (whose sprite i am 100% sure is behind the ones i want to select)
     
  32. the_lemur

    the_lemur

    Joined:
    Apr 3, 2013
    Posts:
    104
    Yes I do have the answer.

    Physics2DRaycaster line 26:

    distance = Vector3.Distance(eventCamera.transform.position, hits.transform.position)
    ^
    This is used by Unity to determine which item gets the click.

    You and I are both moving our cameras, while my tileMap ( and probably yours as well) is static at 0,0,0.

    The object is off at say, 15,4, 3.

    The camera starts out somewhere near the center position at 10,10, say, and is closer to the object. As you pan the camera it eventually becomes closer to the tileMap (0,0,0) and the object is sorted last.

    IMHO this is a mistake of Unity. The object's absolute position should not be taken into account in 2d to determine what object gets hit. The perpendicular distance from camera should be used.
     
  33. the_lemur

    the_lemur

    Joined:
    Apr 3, 2013
    Posts:
    104
    INCIDENTALLY zooming does not actually affect whether a given item is hittable or not. But when you zoom out it is much easier to quickly pan the camera so its transform is in range of the 0,0,0 map and not the item.
     
  34. the_lemur

    the_lemur

    Joined:
    Apr 3, 2013
    Posts:
    104
    I have tested all these assertions by detailed outputs of Physics hits. But just to show that I'm right, consider panning left.. so your map is now on the right edge of the screen. Your camera is now in NEGATIVE x, or very near 0. All the items are further along the x-axis at like 15. The tilemap at 0,0 is returned as the result, not the item.
     
    Last edited: Oct 10, 2016
  35. the_lemur

    the_lemur

    Joined:
    Apr 3, 2013
    Posts:
    104
    I think it would make much more sense for Physics2DRaycaster line 26:

    Instead of:
    distance = Vector3.Distance(eventCamera.transform.position, hits.transform.position)

    to have something like:
    distance = Vector3.Project( hit.transform.position - eventCamera.transform.position, eventCamera.transform.forward ).magnitude;

    Simple dot product with Camera forward would also work as well, or sqrMagnitude for efficiency.

    This is because it doesn't make sense for hit distance to change in 2d when panning is parallel to the camera direction.
     
  36. drHogan

    drHogan

    Joined:
    Sep 26, 2012
    Posts:
    201
    I agree, absolutely well done as a debug mate! I think it also gave me the idea on how to hack the problem in my game till they don't fix it in unity
     
  37. the_lemur

    the_lemur

    Joined:
    Apr 3, 2013
    Posts:
    104
    yeah there are several fixes. can just manually run the hits and projections. it's just hacky and I hope they change the way the computation is done.
     
  38. the_lemur

    the_lemur

    Joined:
    Apr 3, 2013
    Posts:
    104

    There are really just so many cases where this comes up in Pointer events, is there a way to report this for an official fix?
     
  39. AssatorGameMakers

    AssatorGameMakers

    Joined:
    Dec 14, 2017
    Posts:
    1
    The problem is still actual