- Home /
life system.....
ok guys a big question
i want to make a live system
i have different weapons with a var called damage
now i want that if my Character is hit there should be a var that is reduced with every hit and than there should be a number in the game view in the corner that is maybe 100 and with every hit it will be reduced
and the final thing is that if different parts of my character are hit there should be more or less damage
i hope you can help me
Answer by spinaljack · Apr 18, 2010 at 08:10 PM
Download the 3D Platform tutorial
There's a section for taking damage, storing lives and health, regaining health and respawning.
Answer by Peter G · Apr 18, 2010 at 09:40 PM
You basically want to have a function that applies damage on the character and have the enemy call that function.
A script like this goes on the enemy:
var damage = 10;
function OnCollisionEnter (col : Collision) { if(col.gameObject.CompareTag("Player") { col.gameObject.BroadcastMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReciever); } }
Then something like this goes on your player.
var health = 25;
function ApplyDamage (damage : int) { health -= damage;
if(health <= 0) {
Die ();
}
}
function Die () { Respawn ();
}
Now causing your player to take more or less damage at different spots is a little more complicated.
I would put colliders on the player in several smaller parts ie. the head and body. So the head collider is on the head gameObject and the body is on the root object. Then have the enemy find the tag of the object it hit and if it is a special part like the head, you would send a different damage in the Message.
var player = GameObject.FindWithTag("Player"); //Find the player so that if we hit the head we can still apply damage to the player. var headDamage = 25;
if(col.gameObject.CompareTag("Head") { player.BroadcastMessage("ApplyDamage", headDamage, SendMessageOptions.DontRequireReciever); }
//add the top script in appropriately.
P.S. I haven't tested the script so there might be a typo or two.
Answer by duck · Apr 18, 2010 at 09:51 PM
Here's a question and answer which discusses how you can adjust a numeric variable, and display that value on-screen:
How to increment a global counter variable (eg, a hit tally) and display in a GUIText