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 Sprakle · Jul 14, 2013 at 10:34 AM · explosiondecalspherecast

Getting points within a radius to place burn decals from an explosion

This image should illustrate what I want: alt text

Here is the code I have at the moment:

 public void MakeBurns(Vector3 position)
 {
     // find all objects within radius
     RaycastHit[] hits = Physics.SphereCastAll(position, burnRadius, Vector3.down, 0);
 
     if (hits.Length < 1)
         return;
 
     // burn each hit
     foreach (RaycastHit hit in hits)
     {
         Vector3 point = hit.point;
         Vector3 normal = hit.normal; // I'm fairly certain this is the wrong normal, but I'll figure that out later
 
         // move away from point
         point += normal.normalized*offsetFromHit;
 
         pool.LoadObject(point, normal);
     }
 }


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

2 Replies

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

Answer by robertbu · Jul 14, 2013 at 03:15 PM

I can think of two ways of approaching this problem. For the first one you start with a Physics.OverlapSphere(). This will give you a list of game object where the bounding box for the collider is within your explosion area. Then you will need to process the mesh to determine the closest point on that game object to the center of the explosion. Here is a link with some source for calculating that point:

http://answers.unity3d.com/questions/424974/nearest-point-on-mesh.html

Note you will need to check the distance to make sure it is within the sphere distance since OverlapSphere() bases its results on the bounding box.

The second solution is to do a sphere of raycasts. Here is one question dealing with that issue:

http://answers.unity3d.com/questions/410992/how-do-i-get-raycasts-to-cast-symmetrically.html

The UniformPointsOnSphere() will generate the directions for all the raycasts. The numberOfPoints will determine the number of raycasts you do. If you only want want one decal per object, you will need to sort through the result and find the closest hit for each object hit.

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 robertbu · Jul 14, 2013 at 03:22 PM 0
Share

Just though of a possible third way. After using OverlapSphere(), you can use Collider.ClosestPointOnBounds() to find the closest point on the bounding box. Then you can do a Collider.Raycast() using the explosion center as the start and the point returned by ClosestPointOnBounds() to deter$$anonymous$$e the direction. It is not as exact as the first idea above, and there are some degenerate cases, but it should work well.

avatar image Sprakle · Jul 14, 2013 at 05:54 PM 0
Share

These are some really great ideas! I've marked this as correct, but I can't vote up with by current rep.

avatar image
0
Wiki

Answer by Sprakle · Jul 15, 2013 at 01:58 AM

Using Robertbu's advice, I created this. When using 10 raycast accuracy, it took an average of 5.2E-5 seconds to complete, and when using 1000 casts, it took on average 0.07 seconds (these times do not include the pool loading). I found I good results with 20 raycasts.

I will later add script to scale up and fade burn marks depending on the distance from the explosion, but that should be easy.

 // (found at http://answers.unity3d.com/questions/410992/how-do-i-get-raycasts-to-cast-symmetrically.html)
 Vector3[] rayDirections = UniformPointsOnSphere(raycasts);
 
 // ALL hits on each object hit
 var allHits = new Dictionary<GameObject, List<RaycastHit>>();
 
 // find all objects to burn
 foreach (Vector3 dir in rayDirections)
 {
     // move dir back a tiny bit so the rays can hit objects at the given position
     Vector3 backedUpPos = position - dir*0.1f;
 
     RaycastHit hit;
     if (Physics.Raycast(backedUpPos, dir, out hit, burnRadius)) // may need RaycastAll
     {
         GameObject hitObject = hit.collider.gameObject;
 
         // if the object doesn't have a list already, add it
         if (! allHits.ContainsKey(hitObject))
             allHits.Add(hitObject, new List<RaycastHit>());
 
         // add the hit the the right list
         List<RaycastHit> list = allHits[hitObject];
         list.Add(hit);
     }
 }
 
 // find the closest point for each gameobject
 List<RaycastHit> burnHits = new List<RaycastHit>();
 foreach (List<RaycastHit> hits in allHits.Values)
 {
     RaycastHit currentClosest = hits[0];
     float closest = Mathf.Infinity;
 
     // for each hit of this gameobject
     foreach (RaycastHit hit in hits)
     {
         float distance = Vector3.Distance(hit.point, position);
         if (distance < closest)
         {
             currentClosest = hit;
             closest = distance;
         }
     }
 
     // burn!
     Vector3 hitPos = currentClosest.point + currentClosest.normal*offsetFromHit;
     pool.LoadObject(hitPos, currentClosest.normal);
 }
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

15 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

Related Questions

How do I make bullet marks? 1 Answer

How to Generate flat uvs 1 Answer

Bullet holes (decals?) on Characters 1 Answer

Unity 5 Broke my shader? 0 Answers

How to shoot bullet wounds onto skinned mesh? 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