Search Unity

Create a button that appears when an ObjectTarget is detected AND uses its position as reference

Discussion in 'UGUI & TextMesh Pro' started by JoanMM, Dec 11, 2018.

  1. JoanMM

    JoanMM

    Joined:
    Nov 29, 2018
    Posts:
    8
    Hi everyone,
    I am new at Unity so this might have a very simple solution, but I have been trying to figure it out for hours without success.

    I am trying to make that when a ObjectTarget is detected, a button appears and it has a fixed position in reference to that object. The idea is that when the object is detected, a button with a "+" is shown, and if one clicks, a menu should open. That menu could be always at a fixed position at the screen, but the button should be always next to the object.

    Until now I managed that the button appears when the ObjectTarget is detected, but this button moves with the camera, so it is not fixed to the position of the object.

    The hierarchy looks like this:
    | ARCamera
    | ObjectTarget
    --| Canvas
    ----| Button

    Alternatively, if I just use a Text instead of a Button, its position is referenced to the Object. However, I did not find a way to interact with that Text later on.

    Is there a way to achieve what I am looking for?
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
    you need to set your canvas to world space.
    If setting setting transfrom.position does not work, try to set the anchored position for x and y and the local position for z location instead.
     
  3. JoanMM

    JoanMM

    Joined:
    Nov 29, 2018
    Posts:
    8
    Thanks for the suggestion.

    When I change from Screen Space to World Space, the button does not appear at all, even if the object is detected.

    I didn't get what do you mean by setting transform.position, so this might be the reason. Could you please expaned on how to do it? Where should I change these settings?
     
  4. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,096
  5. JoanMM

    JoanMM

    Joined:
    Nov 29, 2018
    Posts:
    8
    Great! It was a basically a problem of scale.

    Thanks a lot!