- Home /
How to Make A 'Punch' Remove Health
Hey Everyone. I have a rig set up already and I have a punch and movement animation ready, but I need to make the collision of the Capsule Collider in the hand decrease the health of the enemy hit. I have a health system set up already, but I'm not sure how I would do this.
Any help would be greatly appreciated, even if it's just a link to a video that answers what I need.
Thanks, Del
Answer by ItsaMeTuni · Oct 30, 2017 at 11:19 PM
Create a script called DamageDealer and attach it to the hand (where the Capsule Collider is).
public class DamageDealer
{
public float damage = 10;
private void OnCollisionEnter(Collision collision) //This is executed when a collider attached to the game object this script is attached to collides with something
{
if(collision.gameObject.GetComponent<HealthComponent>()) //Check if the hit game object has the health component
collision.gameObject.GetComponent<HealthComponent>().ApplyDamage(damage);
}
}
Just adapt the code to your health system.
Feel free to ask anything. :)
Hope it helps!
Answer by Deltros · Oct 31, 2017 at 06:58 PM
I tested all of it out but nothing happens, and Im unsure why... I've edited the variables and identifiers
My Health Code
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Health : MonoBehaviour {
public const int maxHealth = 100;
public int currentHealth = maxHealth;
public void ApplyDamage(int damage)
{
currentHealth -= damage;
}
}
Your Code @ItsaMeTuni
using UnityEngine;
using System.Collections;
public class HealthTake : MonoBehaviour
{
public int damage = 10;
private void OnCollisionEnter(Collision collision) //This is executed when a collider attached to the game object this script is attached to collides with something
{
if(collision.gameObject.GetComponent<Health>()) //Check if the hit game object has the health component
collision.gameObject.GetComponent<Health>().ApplyDamage(damage);
}
}
Once again, any help is greatly appreciated.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Object Leaves Parent's Trigger During Parent Movement 2 Answers
UnityScript to C# conversion help? 1 Answer
Boosting Character Controller Problem 0 Answers