- Home /
player damage script help
hello i have made a script for the health of my player. but i can't make a script for te damage can you help me please
this my script
var curHealth : int = 100;
var maxHealth : int = 100;
var healthtext : GUIText;
function Start ()
{
healthRegen();
}
function Update ()
{
healthtext.text = curHealth + " / " + maxHealth;
if(curHealth < 0 )
{
curHealth = 0;
}
if(curHealth > 100)
{
curHealth = 100;
}
if(Input.GetKeyDown("e"))
{
curHealth -= 10;
}
}
function healthRegen ()
{
for(i=1;i>0;i++)
{
yield WaitForSeconds(0.5);
if(curHealth < maxHealth)
{
curHealth++;
}
}
}
First off, welcome to Unity. Second, this isn't meant to be snarky, but that's not your script, it's JesseEtzler's from Youtube. Credit where credit is due. That said, making a damage script really isn't difficult. Pay attention to whatever you titled this one. Caps count! Then, in the script you want to inflict the damage from you want to call your curhealth, which simply means "current health." Here's how that line would look:
Scripttitle.curhealth
that's calling the script, and the component inside of the script called "curhealth". So it would look something like this:
Playerhealth.curHealth -= damage;
Ok, so what the heck is that thing there called "damage"? glad you asked. That's referring to a variable, or something that you can change in the inspector. (so that you can use this same script on multiple enemies with different damage based on the enemy.) You could just make this a number, ins$$anonymous$$d of using the damage variable, but to each their own.
So what you're looking at is you need a separate damage script, attached to a gameobject, be it an enemy or an enemy's weapon. How you receive the damage is up to you, it can be via onTriggerEnter, onCollissionEnter, whatever. $$anonymous$$ake sure that if you use triggers that "Trigger?" is checked on any collider. Hope that gives you some help. God bless.
PlayerHealth.curHealth
would be calling the variable curHealth as if it were static. To call something non static, but simply public, you need to use GetComponent.
ah, see, I moved all my vars to static so they could be called from elsewhere. (clearly could do that other ways, just that's the first way I learned) :)
Understood, just remember that easy access isn't the only reason static exists, static variable can only have ONE instance, and most of the time needs to be reset manually, so it can get messy if misused. I usually only use static if I'm doing Editor scripts, or for classes that derive from System.Object ins$$anonymous$$d of $$anonymous$$onoBehaviour. I definitely would recommend using GetComponent when accessing variables from other scripts or components. I actually started using static for the same reasons you probably did, then found out why I shouldn't, at least in Unity.
It's all good man, you're doing quite well so far. You put more effort into asking your questions than many, and you're always specific and helpful. I just like to point others in the right direction. Now that you know to look more into GetComponent ins$$anonymous$$d of using static, that's a step forward ;)
Answer by clunk47 · Oct 11, 2013 at 06:32 PM
I'm not sure you asked your question in the correct manner... This script you have written works. I press 'e', health is subtracted, then regeneration causes health to rebuild...
EDIT: I've taken out the for() loop (Thx @fafase):
#pragma strict
var curHealth : int = 100;
var maxHealth : int = 100;
var healthtext : GUIText;
function Start ()
{
healthRegen();
}
function Update ()
{
healthtext.text = curHealth + " / " + maxHealth;
if(curHealth < 0 )
{
curHealth = 0;
}
if(curHealth > 100)
{
curHealth = 100;
}
if(Input.GetKeyDown("e"))
{
curHealth -= 10;
}
}
function healthRegen ()
{
while(true)
{
yield WaitForSeconds(0.5);
if(curHealth < maxHealth)
curHealth++;
yield WaitForEndOfFrame();
}
}
The healthRegen is kinda weird. An infinite for loop, I'd rather go with a InvokeRepeating, just a matter of taste though.
I guess the point is just to start a loop that will never end. It starts from 1 and check with 0 which will never happen and hence never stop the loop and constantly refill the health. Still not a good way.
Nice observation. I didn't take the time to rewrite, just to make the for() statement work, but it doesn't even need to be there...
Hey i keep getting a problem with this script it keep saying expecting } found "
Your answer
Follow this Question
Related Questions
Ai Aint Taking Damage From Bullet (FPS Rocket) 1 Answer
Structuring a damage and kill count method... 1 Answer
Damage on collision with player.. 1 Answer
How Can One Collider Recognize Contact With Another? 0 Answers
My enemy wont take damage 0 Answers