Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by icelated · Dec 13, 2012 at 01:55 AM · raycastexplosions

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.

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image shane.rachel · Dec 13, 2012 at 05:11 AM 0
Share

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"?

avatar image icelated · Dec 13, 2012 at 10:25 AM 0
Share

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?

avatar image icelated · Dec 13, 2012 at 10:30 AM 0
Share

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.

avatar image Dreamblur · Dec 13, 2012 at 12:20 PM 0
Share

Either:

  1. Attach a collider to each of your barrels. When the player character enters one such collider, have the barrel explode.

  2. 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.

avatar image shane.rachel · Dec 13, 2012 at 03:12 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Raycast stops when false, even in update() 1 Answer

First Person Controller Target Shooting With Gun 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges