Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Some problems with raycast and buttons [RESOLVED]

Discussion in 'UGUI & TextMesh Pro' started by Yearl, Aug 31, 2015.

  1. Yearl

    Yearl

    Joined:
    Jun 26, 2015
    Posts:
    33
    Hi everybody !

    First time I try to use the new UI, and have two problems !

    1st :
    When I click on a button, the function OnClick work as well but if I hit spacebar, it's like I click again on the button and the function restart ..

    2nd :
    I have some objects behind UI who use raycast .. My game is a RTS and ground is selectable.
    When I click on UI, clicks affect objects behind too !

    If you have any tips, it's welcome !
    Thanks Comm !
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    These are guesses, as I haven't run into these problems myself:

    1st:
    Check Edit -> Project settings -> Input and see if "spacebar" is one of your submit buttons. (check positive and alt positive.. you'll know what I mean when you're in the menu)

    2nd:
    put some additional colliders on your UI that your raycast will hit (make some new gameobjects if you need to). When you do your raycast, check if you hit any of the UI colliders, and ignore your usual raycast logic if you do.


    Hope this helps!
     
  3. Yearl

    Yearl

    Joined:
    Jun 26, 2015
    Posts:
    33
    Ok thanks,

    Yes "sapcebar" is used in my game, i set it for turns pass, so it's an important button, but why this one click and not others inputs setted ?

    Oh great ! I never thought that I can place colliders on UIs objects ..!
    **Just tryied but it's like my raycast pass trought them ..**

    EDIT:
    Just saw when I use arrow keys (camera moving in my game), changing the "nears buttons" I click on HoverState .. And then if I use spacebar, it's click on the button hovered by arrows.
    (Hard to explain)
     
    Last edited: Sep 2, 2015
  4. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    If your raycast is passing through your UI colliders, make sure you're not ignoring the UI layer in your raycast. If the UI is rendered by a different camera than the rest of your game, you'll need to raycast from both cameras. (I recommend raycasting from your UI camera first, so if you find a hit there, you don't have to raycast from your game camera)

    To make sure the "submit" button (your spacebar, in this case) doesn't activate buttons, change the "navigation" dropdown to "none" in the inspector with the button selected.
     
  5. Yearl

    Yearl

    Joined:
    Jun 26, 2015
    Posts:
    33
    Ah thanks a lot for the "none" advice for UIbuttons !!

    I'm not ignoring the raycast (UI/ignoreRaycast disable in physics) and I render it on the basic main camera of the scene but still not working .. I put BoxColliders and SphereColliders on them, that's good ? Don't need 2D colliders ?
     
  6. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    for 2D colliders you need a Physics2D.Raycast(), for 3D colliders you just use Physics.Raycast()
     
  7. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    ubedjunejo and Phobarp like this.
  8. Yearl

    Yearl

    Joined:
    Jun 26, 2015
    Posts:
    33
    Can't get hit with colliders .. 3D or 2D. Make me crazy !

    This solution works only with interractable UI ? So only with buttons and sliders ?
    Or still working with panels and images too ?
     
  9. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,986
    Anything that you want on the canvas.
     
  10. Yearl

    Yearl

    Joined:
    Jun 26, 2015
    Posts:
    33