Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 p38 · Jul 29, 2011 at 09:36 PM · collisionexplosionkeypressbreak

Simple Explosion Script With Key Press

Greetings All,

I need an example of a very simple explosion script.

I've created a prefab rock object that contains smaller rock objects which I am trying to force outward with a key press.

I found this little script that works when the game starts but only on box objects.

Any example is greatly appreciated, suggestions are welcome, and links without detailed explanations don't help a whole lot.

/*

DestructibleObject.js

source: http://answers.unity3d.com/questions/48718/how-can-use-addexplosionforce-to-explode-scattered.html

*/

var radius = 1.0; var power = 50.0; //function Start ()

function OnCollisionEnter(collision : Collision) {

     Debug.Log("start");
     // Applies an explosion force to all nearby rigidbodies
     var explosionPos : Vector3 = transform.position;
     var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
     
     for (var hit : Collider in colliders) 
         {
             // if(collision.gameObject.tag=="Explosion") continue;
             if (!hit) continue;
             /* 
                 not sure why i have to explicity name everything
                 in the scene with a rigidbody 
              */
             if (hit.name=="breakable_rock_normal") continue;
             if (hit.name=="ground") continue;   
             if (hit.name=="MouseCollider") continue;   
             if (hit.name=="Plane") continue;     
             if (hit.name=="Terrain") continue;
             if (hit.name=="altar") continue;
             if (hit.name=="crypt") continue;
             if (hit.name=="graves01") continue;
             if (hit.name=="Main Camera") continue;
             
             if (hit.rigidbody)
             Debug.Log(hit.name);
             hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 10.0);
         }
     
     /* Destroy(gameObject); this does not work... */    
 
 }
Comment
Add comment · Show 1
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 p38 · Jul 29, 2011 at 11:18 PM 0
Share

Still working on it...

Here is what I've pieced together so far. I've managed to move the object in an upward motion, but cannot "explode" my objects in the prefab...

// Initialize Script Variables var radius = 1.5; var power = 100.0; var playerScore : int = 0; var playerTag : String = "The Player Nemo"; var keyPressCount:float=0.0; // keypress counter function Update () {
if (Input.Get$$anonymous$$eyDown ("space")) // if spacebar is pressed { //Debug.Log ("SpaceBar $$anonymous$$eypress ", gameObject);
var bigBoulder = GameObject.FindGameObjectsWithTag ("Big Boulder Test"); // find and assign variable to Big Boulder Test object tag var explosionPos : Vector3 = transform.position; var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
// Debug.Log ("Debug 1", gameObject);
for (var hit : Collider in colliders) { if (!hit) { continue; Debug.Log ("if not hit...", gameObject); }
/
source: http://answers.unity3d.com/questions/21338/a-better-way-then-ifhitgameobjecttag.html
/
if(hit.gameObject.tag == bigBoulder) { playerScore++; // increase player score hit.gameObject.Send$$anonymous$$essage("ApplyPlayerScore",playerScore); // send player score to display function
// Debug.Log ("DEBUG: Player Score, ", gameObject);
keyPressCount++; // increase keypress Debug.Log ("DEBUG: " + keyPressCount, gameObject);
continue;
Debug.Log ("DEBUG: Did Rock Explode??? ", gameObject);
}
if (hit.rigidbody) { Debug.Log ("DEBUG: RigidBody ", gameObject); hit.rigidbody.AddExplosionForce(power, explosionPos, radius, 3.0); } }
}
} // end function Update function ApplyPlayerScore (playerScore : float) { print ("Player Score: " + playerScore); } // end function ApplyPlayerScore

0 Replies

· Add your reply
  • Sort: 

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

2 People are following this question.

avatar image avatar image

Related Questions

Implementing collision and key pressing at once 1 Answer

Different Explosion for Different Collisions 3 Answers

How to create a collider-trigger that will start an animation when you press E 0 Answers

How to make an object explode? 2 Answers

Detonation of an object on collision of character.... 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