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 Matrixmage · Jul 14, 2014 at 07:32 AM · c#raycastsphericalrays

What is the best way to send raycasts out in a sphere? And what resolution do I need to be accurate?

So I'm trying have it that when an explosion happens, a bunch of rays are sent out in a sphere, and depending on the properties of what they hit, they will deduct HP, stop moving, continue moving, etc. So what would the most effective way of doing this be? Also, I'm trying to figure out what resolution of rays I will need to be accurate enough to get an effective reading. Any help would be great!

Edit: I'm trying to make the explosion realisticish, so rather then hurting everything in a radius, the explosion has the possibility to be physically blocked by armor. This is why I'm thinking rays are the best, as I would be using them as "explosion probes", functioning as the moving explosion.

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
0
Best Answer

Answer by Loius · Jul 14, 2014 at 04:07 PM

Try this out for brute-forcing a 360* cast:

     void Update ()
     {
         Ray ray = new Ray();
         ray.origin = Vector3.zero;
         float inverseResolution = 10f;
         Vector3 direction = Vector3.right;
         int steps = Mathf.FloorToInt(360f / inverseResolution);
         Quaternion xRotation = Quaternion.Euler (Vector3.right * inverseResolution);
         Quaternion yRotation = Quaternion.Euler(Vector3.up * inverseResolution);
         Quaternion zRotation = Quaternion.Euler(Vector3.forward * inverseResolution);
         for(int x=0; x < steps/2; x++) {
             direction = zRotation * direction;
             for(int y=0; y < steps; y++) {
                 direction = xRotation * direction;
                 Debug.DrawLine(ray.origin,ray.origin+direction); // for science
             }
         }
     }
 

Resolution's hard to say. You can do more casts if you split them into several frames using a coroutine. You could also do a sphere-overlap, then cast to the corners/center of any target objects. If you hit, great, if you miss, expand the cast zone a bit and try again. (Basically cone-cast towards the target with expanding radius until you're casting outside of the target's bounds or you hit it)

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 Matrixmage · Jul 15, 2014 at 05:47 AM 0
Share

This code is great, but I have no clue at all what its doing, beyond what I can see from the debug lines... I've not looked into rays that much, so I don't really know how they work, and I've only ever seen them used in raycasts. If you could give a $$anonymous$$or description of what it does that would be amazing, if not, tomorrow I have a friend who knows more about this specific stuff and should be able to help. Despite my confusion, thanks a lot!

avatar image Loius · Jul 15, 2014 at 03:15 PM 0
Share

ray is just a directional line. it's got one endpoint and a direction. so you'd set ray.direction = direction in the code above and use that ray for your cast

what this is doing is it's starting with a ray pointed 'right', then rotating it {a} degrees left, then spinning it completely around the X axis by {a} degrees at a time, essentially creating a ray at equal intervals.

the int x actually only has to go up to steps/2, i think. 0-to-stepps is actually double-casting.

avatar image
0

Answer by Andres-Fernandez · Jul 14, 2014 at 07:33 AM

Do you need to use rays? Because maybe you can use overlapSpheres. Check a similar question here, and tutorial here.

Comment
Add comment · Show 3 · 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 Matrixmage · Jul 14, 2014 at 03:41 PM 0
Share

Sorry, I forgot to mention in the post why I was going to use rays versus the overlap sphere, but I've updated the question. Sorry for the confusion!

avatar image Andres-Fernandez · Jul 15, 2014 at 06:54 AM 1
Share

You can use the overlapSphere to check which objects are inside the explosion and then cast rays from the point of the explosion to the objects and see if they are blocked. If ray collides with object then it's hit, if not then it's blocked. That way you avoid casting infinite rays. You only cast the amount of rays you need.

avatar image Loius · Jul 15, 2014 at 05:06 PM 0
Share

overlap+cast-to-object works for smallish objects, but for large things you'll need multiple casts (expanding cones, or you could cast to every other vertex of the target's mesh, something to give you a more scattershot approach)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Clicking on an Object to Make it the Variable Target 1 Answer

Raycasting and Tag Checking in C#? 1 Answer

NPC sight by collider 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