- Home /
OnGUI and Event.current
Hello i am trying to reduce healt and mana while pressing 'H'. I know that for each event in GUI i need to use Event.current instead of Input.GetKeyDown since this is GUI event. Here is the code, when i am pressing H nothing is happening it should reduce hp and sp but it does not. Anyone could tell me why is that please ?
#pragma strict
var curHp : float = 100.0;
var maxHp : float = 100.0;
var curSp : float = 50.0;
var maxSp : float = 50.0;
var hpBarTexture : Texture2D;
var spBarTexture : Texture2D;
var hpBarlength : float;
var percentOfHp : float;
var spBarLength : float;
var percentOfSp : float;
private var _hpSpWindowRect : Rect = new Rect(10,Screen.height / 2 + 150,190,70);
private var _hpSpWindowID : int = 1;
function OnGUI () {
var e = Event.current;
if (e.type == EventType.KeyDown && e.keyCode == KeyCode.H)
ReduceHelathAndMana();
_hpSpWindowRect = GUI.Window(_hpSpWindowID, _hpSpWindowRect, HpSpWindow, "Health points / Spell points");
}
function ReduceHelathAndMana() {
percentOfHp = curHp/maxHp;
hpBarlength = percentOfHp*100;
percentOfSp = curSp/maxSp;
spBarLength = percentOfSp*100;
}
function HpSpWindow (_hpSpWindowID) {
if (curHp > 0)
{
GUI.Box (new Rect (20,20,150,20),hpBarTexture);
}
if (curSp > 0)
{
GUI.Box (new Rect (20,40,150,20),spBarTexture);
}
GUI.DragWindow();
}
Answer by Unitraxx · Mar 28, 2013 at 07:00 PM
Your code inside ReduceHelathAndMana doesn't actually decrese the heatlth. Try this :
function ReduceHelathAndMana() {
curHp = curHp - 10;
percentOfHp = curHp/maxHp;
hpBarlength = percentOfHp*100;
curSp = curSp - 10;
percentOfSp = curSp/maxSp;
spBarLength = percentOfSp*100;
}
(10 can be replaced by a value you want to decrease with)
Yeah I see it's a matter of interpretation of the question. If he wants the value to decrease continously over time you do have to use the Time.deltaTime, but I thought he only wanted to decrease once every time he pressed H.
Answer by TrewSx · Mar 28, 2013 at 07:09 PM
ohh yes i need to subtract form curHp sorry didnt see that, but when i use this and in my scene i am pressing H after 3 or 4 presses the whole health bar disappears. I want to make reduce the health bar bit by bit
Just use a lower value to decrease with? Take for instance my answer, and replace the 10's with 1's.
If you mean that you want it to decrease over time you should not use EventType.keyDown and you should connect Time.deltaTime.
curHp -= 10*Time.deltaTime;
Sorry for my interpretation, English is not my primary language and i do have a problems with explaining stuff in it :). Well i do want to slowly reduce the health and mana values, but when i use your modification the health and mana bar remains in the same length and after few more presses it just disappears. This code below is working as i want it the only difference is that it is not in GUI window + it is not dragable.
#pragma strict
var curHp : float = 100.0;
var maxHp : float = 100.0;
var curSp : float = 50.0;
var maxSp : float = 50.0;
var hpBarTexture : Texture2D;
var spBarTexture : Texture2D;
var hpBarlength : float;
var percentOfHp : float;
var spBarLength : float;
var percentOfSp : float;
function OnGUI () {
if (curHp > 0) {
GUI.DrawTexture (new Rect (50,120,hpBarlength,20),hpBarTexture);
}
if (curSp > 0) {
GUI.DrawTexture (new Rect (50,140,spBarLength,20),spBarTexture);
}
}
function Update () {
percentOfHp = curHp/maxHp;
hpBarlength = percentOfHp*100;
percentOfSp = curSp/maxSp;
spBarLength = percentOfSp*100;
if(Input.Get$$anonymous$$eyDown("h")) {
curHp -= 10;
}
if(Input.Get$$anonymous$$eyDown("h")) {
curSp -= 10;
}
}
Your answer
Follow this Question
Related Questions
Scale GUI Texture From the Center. 1 Answer
[Solved]Label is not a member of GUI anymore. 1 Answer
The first object in selection grid can't be activated. Why? 1 Answer
Custom Inspector Invocation behaviour? 1 Answer
GUI opacity. 1 Answer