Search Unity

Posistion trouble - Health bar above enemy - C#

Discussion in 'Scripting' started by Loff, Dec 2, 2014.

  1. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    Hello!
    I have tried to make a healthbar stay above a target, but it doesn't move more then a few pixels insted of what the target actually moves.

    For example I have tried something like this:
    Code (CSharp):
    1. public Transform This_enemy;
    2.  
    3. void OnGUI(){
    4. GUI.DrawTexture(new Rect(This_enemy.posistion.x, 50, healthbar_texture_w, healthbar_texture_h),healthbar);
    5. }
    I have tried lots of different posistion, but it seems to only move the X-axis that shows in the "building room". The x,y,z that has a low amount like 1,2,3 in the building room while the GUI should be moving 100,200, 300 pixels to keep on top of the target. Sorry if you can't understand what I'm trying to say.
    Could I be using the wrong posistion ? I tried local as well :(

    Extra info:
    - 2D game!
    - C#
     
  2. GetUpKidAK

    GetUpKidAK

    Joined:
    Apr 9, 2013
    Posts:
    84
    Check the API for Rect: http://docs.unity3d.com/ScriptReference/Rect.html

    The x and y positions you're passing are being given as the left and top positions of the rect, so they don't relate the the enemy's position in the scene.
     
    Loff likes this.
  3. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
    Understand! Still not sure how to get access to the correct x,y. Tried to read the manual but I seem to be missing something as I can't understand where to get the correct value.

    Code (CSharp):
    1. This_enemy.posistion.x
    hmm..
     
    Last edited: Dec 4, 2014
  4. sterynkng14

    sterynkng14

    Joined:
    Sep 19, 2013
    Posts:
    36
  5. Loff

    Loff

    Joined:
    Dec 3, 2012
    Posts:
    81
  6. sterynkng14

    sterynkng14

    Joined:
    Sep 19, 2013
    Posts:
    36
    If the object doesn't have a camera attached you could try Camera.main.WorldToScreenPoint() but make sure your camera has the tag of MainCamera
     
    Loff likes this.
unityunity