From d83db808f468d635d41b3651d4c28ac22e22edc2 Mon Sep 17 00:00:00 2001 From: Stefano Calabretti Date: Sat, 1 Dec 2018 18:22:13 +0100 Subject: [PATCH] Updated damage on hit (for player collision) --- Assets/Scripts/DamageOnHit.cs | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/Assets/Scripts/DamageOnHit.cs b/Assets/Scripts/DamageOnHit.cs index db5ba44..4023d88 100644 --- a/Assets/Scripts/DamageOnHit.cs +++ b/Assets/Scripts/DamageOnHit.cs @@ -2,13 +2,29 @@ using System.Collections.Generic; using UnityEngine; -public class NewBehaviourScript : MonoBehaviour { +public class NewBehaviourScript : MonoBehaviour +{ + #region Fields - public int damage = 1; + public int damage = 1; - // Quando il gameObject tocca qualcosa di solido... - void OnCollisionEnter2D (Collision2D collision) { - //Controlla se quello che ha colpito (collision.gameObject) è il giocatore (controlla tipo se il tag è "Player") - //Se sì, trova il suo componente healthController e chiamane il metodo .Damage(int danni) + #endregion + + #region On collision enter - 2D + /// + /// Checks whether the game object has hit + /// the player. + /// + void OnCollisionEnter2D (Collision2D collision) + { + var collidedObj = collision.gameObject; + if (collidedObj.CompareTag("Player")) + { + var healthComponent = + collidedObj.GetComponent(); + + healthComponent.Damage(damage); + } } + #endregion }