Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 pulkit8.mahajan · Jan 27, 2014 at 08:29 PM · explosionbombsolidaddexplosionforce

How to create a time bomb in Unity?

I am making a small arcade game, and I am trying to randomly instantiate a bomb that will explode after a while. But, when it explodes, it doesn't add the explosion force. Here is the code:

 using UnityEngine;
 using System.Collections;
 
 public class Bomb : MonoBehaviour {
 
     public ParticleSystem BombBlast;
     private float timer;
 
     // Use this for initialization
     void Start () {
         timer = Random.Range(5,10);
     }
     
     // Update is called once per frame
     void Update () {
 
         if(timer > 0) 
             timer -= Time.deltaTime;
         else {
         Vector3 position = this.transform.position;
 
         ParticleSystem bombexplosion = (ParticleSystem)Instantiate(BombBlast,position,Quaternion.Euler(100,0,0));
         bombexplosion.Play();
         rigidbody.AddExplosionForce(10000.0f,position,5000.0f);
         Destroy(this.gameObject);
         }
     
     }
 } 

No explosion force is being added. Also, I want to make the bomb solid in the sense that objects should not be able to pass through it. All objects except my player are stopped by it, but my player just goes straight through. How can I stop that?

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Screenhog · Jan 27, 2014 at 08:42 PM

  1. It looks to me as though you are applying a force to a GameObject's rigidbody, and then immediately destroying that GameObject. You need to apply the explosion force to the actual rigidbodies that will show the explosion.

  2. I'm guessing that the player's movements are being handled by directly changing the transform.position of the player? If so, that will ignore physics collisions. You need to be moving the player with a CharacterController or some other way that will pay attention to physics.

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
avatar image
0

Answer by Kiloblargh · Jan 27, 2014 at 08:43 PM

Because rigidbody refers to the bomb's own rigid body, it won't blow up anything else except the bomb.

And it probably won't even do that, since you are applying the force to the bomb's exact center, so the direction it will be pushed is Vector3.zero, IOW nowhere.

It's clear that you do not understand most if any of the script you posted- you are going to have to read the manual, in its entirety, but start with the physics section- if you don't get the syntax, you will also need to read a C# reference. I know it's boring, you want to just jump into getting the game made and blowing things up, but you will never get to that point until you do the boring technical reading part first.

Comment
Add comment · Show 1 · 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
avatar image pulkit8.mahajan · Jan 29, 2014 at 01:39 AM 0
Share

So i did some reading, and I used tags, and worked out the problem. The only thing is now, all the explosions use the same particle system, ins$$anonymous$$d of the 2 different ones I have. Here is my script:

 public void OnTriggerEnter(Collider collider) {
 
 
         EnemyCube = GameObject.FindGameObjectWithTag("Enemy");
 
         Vector3 enemy_position = collider.transform.position;
         
         Destroy(collider.gameObject);
 
         
         Vector3 explosionForce = transform.position;
         Collider[] colliding = Physics.OverlapSphere(explosionForce, 500.0f);
         
         foreach (Collider hit in colliding) {
 
             if (hit && hit.collider.gameObject.CompareTag("Enemy")) {
                 hit.rigidbody.AddExplosionForce(5000.0f, explosionForce, 500.0f);
                 ParticleSystem explosion = (ParticleSystem)Instantiate(EnemyExplosion,enemy_position,Quaternion.Euler(100,0,0));
                 explosion.Play(withChildren:false);
 
             }
             if (hit && hit.collider.gameObject.CompareTag("Bomb")){
                 hit.rigidbody.AddExplosionForce(10000.0f, explosionForce, 1000.0f);
                 ParticleSystem bombexplosion = (ParticleSystem)Instantiate(BombBlast,enemy_position,Quaternion.Euler(100,0,0));
                 bombexplosion.Play(withChildren:false);
 
             }
         }
 
         
     }

How can i use the 2 different particle systems?

avatar image
0

Answer by fafase · Jan 27, 2014 at 08:45 PM

See the docs: http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddExplosionForce.html

In order to apply the force you need to fetch all the rigidbodies and apply the force.

AddExplosionForce is just a shorthand to avoid all calculations of distance from the center of explosion and attenuate using log or linear and so on. The method does it all for you but you still need to pass the explosion value to the rigidbody.

http://www.youtube.com/watch?v=q3lrrPDAHEU

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

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with AddExplosionForce / Exploding 1 Answer

chain of explosions - Destroying gameObjects in range 2 Answers

How can I destroy a object(like house) with a bomb? 0 Answers

AddExplosionForce won't have any effect. 1 Answer

bomb effect 2 Answers


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