Search Unity

Problem with GUI element following enemy

Discussion in 'Scripting' started by Epilol, Jul 31, 2015.

  1. Epilol

    Epilol

    Joined:
    Jul 21, 2015
    Posts:
    85
    Hello guys I'm experiencing a little problem while setting a ui element to follow the enemies. Basically the UI moves with the enemy based on the position of the camera. If the enemy is in the middle of the screen the button is centered, but if the enemy is on the right the button is on the left and if it's on the left, the button is on the right

    enemy on the right of the camera:

    right.png

    enemy on the left of the camera:

    left.png

    and this is the code I'm using:

    Code (CSharp):
    1. Vector3 screenPos = Camera.main.WorldToScreenPoint(unit.transform);
    2. button.tranform.position = screenPos;
     
  2. Epilol

    Epilol

    Joined:
    Jul 21, 2015
    Posts:
    85
    Bump
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Don't bother scripting this. Just make a world space canvas that is a child of the enemy.
     
    Epilol likes this.
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    You should definitely do what BoredMormon says and just make it a child, but personally I think this offset-by-direction-and-distance effect is sort of sexy. Different strokes, I guess.
     
    Epilol and Kiwasi like this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Definitely wouldn't be the first time I've seen an unintended bug become understood and embraced as part of something new and amazing.

    You could also do the code offset in world space, possibly making it simpler.
     
    Epilol likes this.