Destructible Vase/ Barrel

Good afternoon everyone,

Thank you for helping me ahead of time. I have been making art for games for 10 years and I started taking classes this semester to learn how to program C#. I am not asking for handouts, rather, I have the desire to really understand it and its not easy for me to grasp.

I have a beatem’up style game that I am working on. I have created a barrel and a vase that essentially have the same makeup: Player walks up to Vase, attacks it, then vase destroys. The Barrel has a little more HP than the vase does, so I know the Health variable has to be exposed, which it is. I have a 3D Mesh nested inside a game object and ‘isTrigger’ is not checked on.

So, after all that, I have no idea how to get the object to receive damage, nor do I understand how to subtract health when it DOES receive damage.

This is where I’m at; I have also attached an image of the objects Inspector view, see below:

using UnityEngine;
using System.Collections;

public class DestructibleObjects : MonoBehaviour 
{

	// ================================================================
	// This code Determines the Health Parameters of the Object
	// ================================================================

	// Assigning Variables so that I can give the object health.
//	public int maxHP = 100;	
//	public int currentHP = 100; 
//
//	void ChangeHP(int Change)
//	{
//		currentHP+=Change;
//		if (currentHP>100)
//		{
//			currentHP=100;
//		}
//		
//		// If object reaches 0 HP, then destroy. 
//		if (currentHP<= 0)
//		{
//			DestroyMe ();
//			Debug.Log ("HP = 0. Figure out how to destroy the object!");
//		}
//		
//	}

	// ================================================================
	// This code Determines the outcome of the object when destroyed.
	// ================================================================

	// Our Players collider will activate onButtonPress and when it does, it needs to take damage.
	// This inspector field is looking for the Players active collider.
	public GameObject hitDetectionHighCol;

	// Allows for Prefab within Inspector. In our case, we are using the VFX prefab.
	public GameObject debrisPrefab;

	// Play sound when destroyed!
	public AudioClip[] audioOnDestroy;


	//Destroys object on MouseDown, Rather than Punch/Kick from Player.
	//void OnMouseDown ()
//	void OnTriggerEnter (Collider collider)
//	{
//		DestroyMe();
//	}


	// This code can be used in the future when stacking objects or shooting a projectile at it.
	// If this is going to be used, remember to assign a Rigidbody to it, then turn gravity on.
	// Also, make sure your BoxCollider Has 'isTrigger' unchecked.
	void OnCollision (Collision collision)
	{
		if (collision.impactForceSum.magnitude > 1f)
		{
			DestroyMe();
		}
	}


	void OnCollisionEnter (Collision collision)
	{
		if (collision.tag == "hitDetectionHigh")
		{
			DestroyMe();
		}
	}


	void DestroyMe ()
	{
		// Allows me to assign the VFX/ Prefab in the Inspector - COMPLETE and WORKING!!
		if (debrisPrefab)
		{
			Instantiate (debrisPrefab, transform.position, transform.rotation);	
		}
		// - COMPLETE and WORKING!!
		AudioSource.PlayClipAtPoint (audioOnDestroy[Random.Range(0, audioOnDestroy.Length)],transform.position, .75f);
		Destroy(gameObject);
	}
}

[33227-destructible+barrel+inspector+setup+00.png|33227]

It seems like the code is almost there, all that needs to happen is for you to combine what you have right now.

You already check the collision (and it’s working since the vase already destroys) in OnCollisionEnter. Instead of the method DestroyMe, you will have to call the ChangeHP method.

void OnCollisionEnter (Collision collision)
    {
        if (collision.tag == "hitDetectionHigh")
        {
            ChangeHP(-10);
        }
    }

That should just work (make sure to uncomment that method).

Hi,
When i started with colliders i always got messed up…
check this site

At the end you will find a table of what triger what and what collides with what… two box colliders will not trigger anything… you need at least on rigidbody.