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).
The easy way to do this would be to use SendMessage - check out the reference here: Unity - Scripting API: Component.SendMessage
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.