- Home /
I have a problem with implementing a health and death system, help?
Ok, so I am designing a basic game that has creatures chasing you, and you have to jump to dodge them. The creatures are suppose to damage the player when they come into contact with the player, and after ten or so hits, the player dies. I am new to Unity, and have a very limited knowledge on JavaScript. All my code is in JavaScript, so keep that in mind. I have tried all the scripts I wrote, and all the ones I could find on the internet. What I really need is a simple mesh colliding damage system, with health and damage displayed as a variable. The script that I wrote at the start was a single code, applied to the player, and having the variables of "what mesh would cause damage on collision", "health of player", and "damage caused by a single collision of said mesh" (not in those exact words). Can you tell me how you would do it, if I was on the right track, and any tips and tricks involving this sort of collision problem. I want to learn how to do this, so If anyone does post code, add captions to show what each line does.
-Thanks
Answer by Daphoeno · Mar 05, 2013 at 07:18 PM
Try this out. Be sure that your character has an extra collider on it set to trigger then make sure your enemy has a rigidbody and this should work. If you have any problems I will help you :)
var PlayerHealth : int = 100;
var MaxPlayerHealth : int = 100;
var DeathAnimaton : String;
var CurrentLevel : String;
function Start ()
{
PlayerHealth = MaxPlayerHealth;
}
function Update ()
{
if(PlayerHealth >= MaxPlayerHealth){
PlayerHealth = MaxPlayerHealth;
}
if(PlayerHealth <= 0){
PlayerHealth = 0;
animation.Play(DeathAnimation);
Application.LoadLevel(CurrentLevel);
}
}
function OnTriggerEnter (other : Collider)
{
PlayerHealth = PlayerHealth - 10;
}
I have messed around with the script you gave me, with some luck. I removed the death animation, as my game is in first person, but I encountered a problem- In the last few lines, I noticed a reference to a trigger in the OnTriggerEnter command. It's trying to reference to collision with other objects i'm guessing, but nothing occurs when I collide. How would I bind this collision to another certain mesh? The code is not working either, I tested it. I bound it to the entity that would be receiving the damage (the player), but nothing happened, even when I ran into different meshes numerous times.
function OnTriggerEnter (other : Collider) { PlayerHealth = PlayerHealth - 10; }
Could you give a revision of your code and tell me why it is not working this time?
-Thanks
and sorry about the script not showing up right in the comment, ID$$anonymous$$ what is happening
first of all be sure that your enemy has a rigid body on it. If it still doe not work then add a variable for your enemy that is a GameObject then change the OnTriggerEntrance function so it says function OnTriggerEnter (Enemy : Collider) with Enemy being the new variable.
For your other problem your player should have a new collider that is a trigger that is bigger than the player