- Home /
Exploding barrel to kill enemy
I can shoot a barrel and have it explode. I want it to be able to kill the player or an enemy if its close. I am making a first person shooter using C# I am able to explode the barrel by hitting it with ray casting.
I have looked at the unity fps tutorial since someone told me that they do the same thing i currently want to do. However, i looked at the "damage receiver" class that's attached to the barrels and i am not sure how they are doing it. How can i get the exploding barrel to kill a player or enemy if they are close?
using UnityEngine;
using System.Collections;
public class explodingbarrel : MonoBehaviour {
public float MaxHealth = 35;
public float currentHealth = 0;
public Transform explosion;
public float detonationDelay = 0.0f;
public float DamageRadius = 8.0f;
public int maximumHitPoints = 50;
Transform targetPlayer;
Transform targetEnemy;
private Transform myTransform;
private HealthStatusBar healthbar;
playerhealth health;
ZombieDamage2 _zombie;
public float player_distance;
public float enemy_distance;
Transform targetbarrel;
void Awake (){
myTransform = transform;
}
void Start () {
currentHealth = MaxHealth;
targetPlayer = GameObject.FindWithTag("Player").transform;
targetEnemy = GameObject.FindWithTag("Enemy").transform;
targetbarrel = GameObject.FindWithTag("Barrel").transform;
}
public void damage(float damage)
{
if(currentHealth < 0){
return;
}
currentHealth -= damage;
if(currentHealth ==0){
//Destroy(gameObject);
Invoke("DelayedDetonate", detonationDelay);
}
}
public void DelayedDetonate(){
BroadcastMessage("Detonate");
}
public void Detonate(){
Destroy(gameObject);
if(explosion)
Instantiate(explosion, transform.position, transform.rotation);
}
}
I think i need to look into colliers.
Keep in mind i have multiple enemies.
Did you take this from fps tut and apply to a zombie game? There are variables here that aren't a part of the tut projects.
What do you mean by "I am able to explode the barrel"?
This isnt the fps tutorial. I can shoot and hit objects. I have enemies i can kill. I can shoot and hit a barrel and when its hit points are zero i can instantiate an explosion. Is that clear enough for you?
What i was saying is i noticed the fps tutorial does the same thing i want to do not that i copied all the code into a zombie game.
Either:
Attach a collider to each of your barrels. When the player character enters one such collider, have the barrel explode.
Attach a collider to your player character. When a barrel enters that collider, send a message from your player character to the barrel telling it to explode.
add this to script where your barrel explodes from damage before the Destroy(gameObject)
collider.isTrigger = true;
attach a capsule collider (or whatever collider you want to use) the size of the radius you want the explosion to effect. Set isTrigger to false in the inspector.
on your barrel you can set an OnTriggerEnter(Collider other){}
other will be anything that is in or touching the collider at the time. You will have to work with IgnoreCollision or IgnoreCollisionLayers for while the barrel isn't exploding.
In OnTriggerEnter you need to have variables that you can retrieve by your player and enemy that will effect their health. You can try attaching the OnTriggerEnter function on your player and enemies as well using collider ins$$anonymous$$d of other in parentheses.
Answer by HumanError · Dec 13, 2012 at 05:29 PM
I will give you what I did with my bomb:
var lives = 1.0;
var livesCount = 0.0;
var spawnPoint : Transform;
var expl : Transform;
var explodeDelay = 3.0;
var delayTime = 0.0;
var explRange : double;
var c : int = 0;
function Update ()
{
delayTime += Time.deltaTime;
if (delayTime >= explodeDelay)
{
var colli : SphereCollider = GetComponent (SphereCollider);
var rig : Rigidbody = GetComponent (Rigidbody);
if (livesCount < lives)
{
if (c < 1)
{
var expEffect = Instantiate(expl,spawnPoint.transform.position,Quaternion.identity);
}
gameObject.tag = "explosion";
rig.velocity = Vector3.zero;
c += 1;
livesCount += Time.deltaTime * explRange;
colli.radius = livesCount;
}
else
{
Destroy(gameObject);
}
}
}
I put in my bomb two colliders: a mesh collider for the collision with the barrel and a sphere collider for the collision with the explosion. the sphere collider is smaller then the mesh collider so you can only have a collision with the mesh collider.
What I did is to make to shpere collider get bigger by the time so it you hit this collider and not the mash, and it will get to any range you want (input "explRange" in the inspector).
Add a tag to the barrel and add a collision function in each enemy and in your character for this tag.
One bug in this code that I wasn't able to solve is that when you touch the barrel, even if it not exploding, you will still lose health points because of the tag, so this code is not good enogh...
Hope it will help, sorry for poor english.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
C# Raycast 2D GameObject follow Mouse 1 Answer
Finding Distance between Angles and Points 2 Answers