0
void Update () {
    if (!createLineupPanel.activeSelf) return;
    if (Input.GetMouseButtonUp(0))
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        Debug.Log(1);
        if (Physics.Raycast(ray, out hit))
        {
            Debug.Log(2);
        }
    }
}

The console only logged 1 but no 2 when I was clicking.

I used this function before and it worked but I don't know why it doesn't work this time. Is there any additional things that I need to post?

--- Update ---

Panel is under Hierarchy -> Create -> UI -> Panel

enter image description here

Seaky Lone
  • 992
  • 1
  • 10
  • 29
  • Check the start position of the ray using `Debug.Log`. Maybe your camera is too close and the ray starts behind the object you expect to hit. Also If it is a 2d game, try Physics2D.Raycast – Basile Perrenoud Mar 29 '18 at 08:23
  • I flagged one of your [other](https://stackoverflow.com/questions/49354657/c-sharp-onmouseup-not-working) question as duplicate and helped you out in the comment section to figure out how to detect clicks on object. Why did you decide to use raycast? – Programmer Mar 29 '18 at 09:56
  • Is there an actual object under your mouse position? If there isn't anything there to hit then the Raycast won't return true... – Dtb49 Mar 29 '18 at 18:21
  • @Programmer Yes I managed to detect clicks at that time. But I was using 3D. And this time I guess I used 2D and the same strategy doesn't work now. – Seaky Lone Mar 29 '18 at 18:26
  • @Dtb49 How do I know if it hits something? I used Debug.drawray but no ray came out. – Seaky Lone Mar 29 '18 at 18:28
  • You know it hits something when it returns 2 lol. Try putting an empty cube or something in the scene, put your mouse over it and click it. If that doesn't work, then it is something else that is causing the problem, not this specific code fragment. – Dtb49 Mar 29 '18 at 18:31
  • @Dtb49 Wait. Does Raycast hit UI elements? – Seaky Lone Mar 29 '18 at 18:37
  • Yes, as long as you haven't turned it off for that element. – Dtb49 Mar 29 '18 at 18:44
  • @Dtb49 It does hit the cube. But for everything else, no. It logs false for everything else and true for the cube. – Seaky Lone Mar 29 '18 at 18:52
  • Sorry. I have to flag as a duplicate again. It works for both 2D and 3D. The only change to make is to attach `Physics2DRaycaster` to the camera instead of `PhysicsRaycaster`. That's it. There is when raycast is needed but not in your case. See #7 from the duplicate. – Programmer Mar 29 '18 at 21:54
  • Note that if this is a UI element such as the `Image` or `RawImage` component then see #1 from the duplicate. That duplicate handles all type of clicks on components in Unity. Just read it. `OnPointerClick` is used for this. There is a difference between 2D and UI Objects. If you are not sure, post a screenshot of the object's Hierarchy tab. – Programmer Mar 29 '18 at 22:08
  • @Programmer What about panel? – Seaky Lone Mar 29 '18 at 22:18
  • No component called Panel in Unity. Post the screenshot of the "Panel" Hierarchy tab and I will tell you what it is – Programmer Mar 29 '18 at 22:34
  • @Programmer Please check it out in the Update. Thanks! – Seaky Lone Mar 29 '18 at 22:43
  • It's an `Image` component The GameObject is simply named Panel. This is confusing to new Unity users. A panel is just an `Image` component that is stretched to cover the whole UI element screen. See #1 from the duplicate. `OnPointerClick` should be fine. – Programmer Mar 29 '18 at 22:49
  • @Programmer Is there a function like OnPointerOver for UI objects? I want to show some info when the mouse is over something, but some how the OnMouseEnter doesn't work very well. Since I destroy the objects when OnMouseExit is triggered, the info interface flashes all the time. – Seaky Lone Apr 03 '18 at 05:05
  • Yes, `OnPointerOver` should work for UI objects.**Every** function from **#1** in that duplicate should work for UI, 2D or 3D. That's the good side of the using EventSystem. Also it works for mobile devices too without any modification. Please the example from that duplicate – Programmer Apr 03 '18 at 06:39
  • @Programmer But there is no such class when I add IPointerOverHandler. And I also googled OnPointerOver and there is no results. And you example also doesn't have it. – Seaky Lone Apr 03 '18 at 16:43
  • I apologize. It is `OnPointerEnter` and there is also `OnPointerExit`. If you actually read the duplicate, you would have figured this out. – Programmer Apr 03 '18 at 19:04
  • I am actually using OnPointerEnter and OnPointerExit but they don't work quite well. I am implementing a function that can show the info when mouse is on something and destroy the info when mouse exits. However, sometimes the info window is flashing when the Pointer enters. So I wonder if I can use something like OnPointerOver to avoid this problem. – Seaky Lone Apr 03 '18 at 20:12

1 Answers1

0

I'm used to deal with it like this:

var mousePositionWorld = camera.ScreenToWorldPoint(Input.mousePosition);    
return Physics2D.Raycast(new Vector2(mousePositionWorld.x, mousePositionWorld.y), Vector2.zero, 0f);

(You mention "2d-tools" in the tags, so I assume you actually need 2D raycasting.)

Synthetic One
  • 405
  • 2
  • 9
  • I enabled 2D in the scene view, do I need 2D raycasting? I have watched some video tutorials and they all enabled 2D in the scene view but they didn't use 2D raycasting. They just use Physics.Raycast. – Seaky Lone Mar 29 '18 at 04:43
  • @SeakyLone `Physics` is for 3D raycasting. `Physics2D` is for 2D raycasting. They're entirely different physics engines. Additionally, in your question you had `out hit` and never declared a variable named `hit`. – Draco18s no longer trusts SE Mar 29 '18 at 06:46
  • for 3d raycasting you need to have Physics Raycaster on your camera. For 2d raycasting you need to have Graphics Raycaster on the canvas. As the canvas is effectively a flat isometric view, there's no need to cast mouse cursor from the camera either, I believe input position is already expressed in canvsas space (in overlay) – zambari Mar 29 '18 at 07:48
  • @Draco18s I did. It is my private variable. I just didn't post it. – Seaky Lone Mar 29 '18 at 18:20
  • @zambari But how do I know which gameobject is clicked or dragged? – Seaky Lone Mar 29 '18 at 18:21