- Home /
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.
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)
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!
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.
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.
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!
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.
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
![](https://koobas.hobune.stream/wayback/20220613154011im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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