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
2
Question by ryan123321 · Sep 30, 2012 at 09:16 PM · raycastbreakglassthanks

Glass break effect

I was wondering if you could make a script in java or c sharp that takes a object and divides it as if it were glass,if it is hit by raycast.Thank you for reading this and have a great day.

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 Fattie · Oct 01, 2012 at 06:50 AM 1
Share

in video games it is very common that you: swap an object out, for another version of the object. for example, perhaps the "dead version" or the "broken version" or the "exploded version" or the "high-power version"

often when you're playing a video game, the objects are actually totally swapping out for new ones, even if you don't realize it.

in this example, before hand you make the "normal" object and you make the "shattered" object which will proabbly have a few pieces. to shatter it you just swap from the normal to the "shattered version"

as you say just Destroy the original object (or more likely simply move it offscreen) and instantiate the new object (or more likely simply move it onscreen from your offscreen pool area

As an incredibly advanced topid, as Aldo hints at you could actually re-engineer objects in real time (modifying the mesh and so on, using artificial intelligence to decide on new shapes and what have you) but that it tremendously beyond what you're asking. what you're asking is just an everyday "swap to a broken version" - so enjoy!!

2 Replies

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

Answer by aldonaletto · Sep 30, 2012 at 09:50 PM

This can be done, since Unity allows access to the mesh, but the results may be deceptive: the mesh division algorithm may take too much time, and also produce too repetitive segments. An intermediate solution could be to create the object with a suitable triangle division, and simply "break" these triangles and convert them to meshes when the object is hit.
But if you want the pieces to fall and bounce like real world objects, forget about this method: you must create a mesh collider for each piece, a slow operation that may cause a hiccup in your game. It's better to previously create the broken version as a prefab and replace the healthy object with it when necessary.

Comment
Add comment · Show 2 · 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 ryan123321 · Sep 30, 2012 at 11:20 PM 0
Share

so quickly instantiate the broken object after the one that isn't broken gets hit.

avatar image aldonaletto · Oct 01, 2012 at 01:45 PM 1
Share

You can instantiate the broken version when the object is hit - it's a simple solution and may work fine enough. But @Fattie is right: when the game becomes too complex, instantiating and destroying objects may cause performance hiccups, thus some pool system is often used. In this particular case, however, the best solution would be to have the broken version already in place, but deactivated: when the glass is hit, deactivate the healthy version and activate the broken one - like this (healthy object script):

var brokenObj: GameObject; // drag the broken version here // glass starts with little health, so that it dies easily private var health: float = 5;

function ApplyDamage(damage: float){ health -= damage; if (health <= 0){ gameObject.SetActiveRecursively(false); // turn off the healthy version... brokenObj.SetActiveRecursively(true); // and turn on the broken one } } NOTE: You must call ApplyDamage in this object when it's hit. You can get the object script and call the function directly, or use Send$$anonymous$$essage, like this

 hit.transform.Send$$anonymous$$essage("ApplyDamage", 10, Send$$anonymous$$essageOptions.DontRequireReceiver);
avatar image
0

Answer by hvilela · Sep 30, 2012 at 11:30 PM

I recommend you to create two objects. One with one mesh, to use before it gets hit and another composed by several meshes, to replace the first one when it get's hit.

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

Multiple Cars not working 1 Answer

When I click, my enemy doesnt lose health... 0 Answers

How can i make the ray cast don't go through the walls 1 Answer

Raycast on Particle - Transform 0 Answers

Draw a ray to deterime if anything is in front of colliding object? 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