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 ayseaktas · Jun 10, 2021 at 09:41 AM · 2dobjectrigidbody2deffectstop-down

How to shatter 2d objects without rigidbody?

Hi! I want to shatter a 2d object, I am looking for options and most of them using shattered prefabs with rigidbodys. But I can't use rigidbody because my game is top-down so there is no collider under the object and i can't use gravity. Is there other options to shatter a 2d object without using rigidbody?

Comment
Add comment · Show 4
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 DenisIsDenis · Jun 10, 2021 at 09:58 AM 0
Share

Rigidbody2D, like RigidBody, has gravity parameters. If your game is 2D from top-down, then in the Rigidbody2D parameters there is gravityScale, which can be set to zero if gravity is not needed.

avatar image ayseaktas DenisIsDenis · Jun 10, 2021 at 10:03 AM 0
Share

But when you instantiate shattered prefab , shattered the parts needs force and gravity to fall. Am i wrong?

avatar image DenisIsDenis · Jun 10, 2021 at 10:12 AM 0
Share

What do you mean by "shattered object"?

avatar image ayseaktas DenisIsDenis · Jun 10, 2021 at 11:26 AM 0
Share

I mean most of the people i saw do it like this; For example there is a box and box's pieces like it shattered. They put together the parts and add rigidbody components to them then save it as prefab. When something hit the box they destroy the un-shattered box and instantiate shattered prefab on originals place. And with rigidbody, pieces starts falling like its shattered. This technic can work when the game is side-scrolled but in my case its top-down so I can't use gravity, I need something else.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DenisIsDenis · Jun 10, 2021 at 12:26 PM

Here is an example script that splits an object into pieces when hitting an object tagged "Bullet":

 using UnityEngine;
 
 public class ShatterGameObject : MonoBehaviour
 {
     public GameObject shatteredGameObject;
 
     private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.CompareTag("Bullet"))
             return;
 
         var shatteredObj = Instantiate(shatteredGameObject, transform.position, transform.rotation);
 
         var rigidbodies = shatteredObj.GetComponentsInChildren<Rigidbody2D>();
 
         foreach (Rigidbody2D rb2d in rigidbodies)
         {
             Vector2 force = rb2d.transform.position - collision.transform.position;
             rb2d.AddForce(force.normalized * 100);
         }
 
         Destroy(gameObject);
     }
 }

Making a simple scene I got the following result (GIF): GIF1


Main Object and Shards parameters (GIF): GIF2 (Note: I have increased the LinearDrag value to slow down the shards over time).


shards.gif (87.7 kB)
parametrs.gif (61.0 kB)
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 ayseaktas · Jun 11, 2021 at 10:40 AM 0
Share

Thank you so much, it worked very well! Especially the pieces seem to be scattered all over the floor and this looks realistic. Thank you very much for taking the time to solve this problem :))

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

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

Related Questions

Avoiding 2D top-down diagonal movement 2 Answers

Blocking object from going through colliders (no rigidbody) [2D] 1 Answer

8-directional orientation, top down 2D, seperate from movement 0 Answers

Scaling Script? 1 Answer

Top Down shooter Moving while Shooting 0 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