unity3d - Unity 2D - HP bar -
the value of hp bar drops when runner collides obstacle. of course, value of hp drop nomally, image of hp bar not change. why error?
public class csrunner : monobehaviour { public vector2 jumpvelocity; public float _hp = 100f; public float _curhp; bool isjump; public image _hpvalue; bool collision_box; // use initialization void start() { _hpvalue = gameobject.find("hpbar").getcomponent<image>(); _curhp = _hp; } // update called once per frame void update() { _hpvalue.fillamount = _curhp / _hp; if (input.getkeydown(keycode.space) && isjump) { isjump = false; transform.getcomponent<rigidbody2d>().addforce(jumpvelocity/2, forcemode2d.impulse); } if ((input.getkeydown(keycode.space) || input.getmousebuttondown(0)) && collision_box) { isjump = true; transform.getcomponent<rigidbody2d>().addforce(jumpvelocity, forcemode2d.impulse); } else { getcomponent<animator>().settrigger("run"); } } void ontriggerenter2d(collider2d coll) { if (coll.transform.tag == "enemy") { _curhp--; _hpvalue.fillamount = _curhp / _hp; } } }
here screenshot of scene :
thank !
Comments
Post a Comment