- Home /
Other : OP is asking for scripts.
Player Health
I am making a game where I have a player tagged "Player" and an enemy tagged "enemy". I want to deduct health away from the player so that the player dies in 10 seconds. health will be deducted from the player when he collides with the enemy. I just need a script for the player as I all ready have a script to handle killing they enemy.
Are you asking: Can you make me this? or are you asking: How do I make this?
I am using java, however I need my project done soon and I don't have time to learn java. can someone write it?
Why not just search for a tutorial ins$$anonymous$$d of asking for someone to make it for you? Anyways, here's a good one. http://www.burgzergarcade.com/hack-slash-rpg-unity3d-game-engine-tutorial. I know it's in C#, but all you'll need to change really is the variables and add function(). Or, you could just use C# since I doubt you have much scripted.
Java is something completely different, don't look for tutorials on that.
Answer by DryTear · Mar 13, 2013 at 01:53 AM
in Javascript;
var cTime : Int = 10f;
function OnTriggerEnter(me : Collider)
{
if(me.gameObject.tag == "Player")
{
yield WaitForSeconds(cTime);
Destroy(me.gameObject);
}
}
Answer by Auggie · Mar 15, 2013 at 05:51 AM
here:
var health = 10;
var i;
function OnTriggerEnter( something : Collider )
{
var timer=10;
if(something.gameObject.CompareTag("Enemy"))
{
for ( i= 0; i < timer; i++){
health--;
yield WaitForSeconds(1);
//this will deduct 1 health per second for 10 seconds
}
}
}
function OnTriggerExit( something : Collider )
{
i = 10;
}
again. add this to the player and add rigidbody to both player and enemy.
please upvote so that we'll be done here.
This is a kinda unsafe way to do this, by the way. Consider what happens if you collide with two enemies, then stick like glue to one but move away from the other.
Also note that you aren't checking what it is you're moving away from.
true. it's not a fool proof code. it's just a skeleton. i made it as simple as i can and as quickly as i can...hehehe...it's just to get him started
Since he's asking for people to do his homework he probably doesn't care anyway, right? $$anonymous$$ake him work for his A! >:D
lol do it yourself because this site is for questions not for homework answers. Learn
Answer by nixcs2512 · Mar 15, 2013 at 09:16 AM
here in JS:
var PlayerHealth: float = 10.0;
var EnemyDamage: float = 1.0;//(damage per sec)You may change it to kill player quickly
function OnTriggerStay(enemy: Collider){
if(enemy.CompareTag("Enemy"))
{
PlayerHealth-=EnemyDamage*Time.deltaTime;
}
if(PlayerHealth<0)
{
Debug.Log("Player died.You lost!");
Destroy(gameObject);
}
}
Remember to add Rigidbody to your character.
Thanks for not being such a jerk like everyone else. you can't learn Javascript without the help of people like you. The way you wrote it makes perfect sense. Thanks a ton bro!
What has to happen? I added it, and the enemy just came to me, and circled around me, nothing happened...
that's basically what it does. but when your playerhealth becomes zero a message in your debug console will say: "Player died. You Lost!"...you need to add something else in the: if (PlayerHealth<0) { //add code here to make me do something. }
to make is do something. afterwards, the player object will be destroyed.
@Tyler$$anonymous$$J The only person being a "Jerk" here is you! For expecting an answer for a question that doesnt not fit the requirements posed by UA. You are the "Jerk", because you expect to learn without putting in any effort what so ever yourself. You can't learn JavaScript by just reading other peoples code! DO IT YOURSELF!!
Follow this Question
Related Questions
Player Character Health 2 Answers
Enemy not taking damage on collisions. 2 Answers
Taking a hit 3 Answers
Getting enemy to move towards player untill they collide problem... 2 Answers
Lose health on collision 1 Answer