- Home /
Question on Player Damage
I have a regular enemy damage script, but I am wondering, I am planning on making a paintball minigame, and how can I take damage from another player? (NOTE: The game will be an online game).
How are you doing the enemy damage? I'm assu$$anonymous$$g the player cannot shoot their self, so you should be able to attach the same damage script to the player. Where it would get more complicated is if you had $$anonymous$$ms and you wanted to keep players from damaging their $$anonymous$$mmates.
Answer by refardeon · Sep 24, 2012 at 07:05 PM
The easy way to do this would be to use SendMessage - check out the reference here: http://docs.unity3d.com/Documentation/ScriptReference/Component.SendMessage.html
Basically, you need a function called "ApplyDamage" or similar on every object that can be damaged:
public void ApplyDamage (float damage) {
hitpoints -= damage;
if (hitpoints <= 0f) {
Die();
}
}
On your paintballs, you'd have something like this:
void OnCollisionEnter(Collision collision) {
float damage = 5.0f;
collision.gameObject.SendMessage("ApplyDamage",
damage,
SendMessageOptions.DontRequireReceiver);
}
(Sorry for the indentation, otherwise some of the code gets cut for some reason)
This calls the function "ApplyDamage" on the object just hit, where 5 points of damage are dealt. The last parameter (SendMessageOptions) ensures that there won't be an error if the paintball collides with an object without the ApplyDamage function, such as a wall.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Problem with enemy health. 1 Answer
Player Damage to enemy damage melee system 0 Answers
Dungeon Enemy Help 1 Answer
Player attacks once but the enemy takes double damages! 1 Answer