- Home /
Shoot off multiple raycasts from 1 object?
So I have a gameobject that I want to shoot off multiple raycasts, lets say 50 raycasts in 50 random directions.
Then IF it hits a collider with a set tag it applies force.
how couls I go about doing this?
Thanks
Answer by perchik · Aug 02, 2013 at 04:38 PM
I would probably use Random.insideUnitSphere inside of a loop to create and cast 50 different rays. The collider part is trivial if you've read the docs.
Answer by fafase · Aug 02, 2013 at 04:44 PM
You can use the center point of your object and rotate around it by a certain degree.
float angle = 360 / frequency;
Quaternion quat = Quaternion.AngleAxis(angle,Vector3.forward);
Vector3 vec = transform.forward;
for (int i = 0; i <=360 ; i = i + frequency ){
// Regular raycast using vec and transform.position
vec = quat * vec;
}
The frequency will determine how many raycast you get, now many raycast may hit so you should store them in a list and find the sortest hit.distance to figure out which one is the closest to the other object.
Or you can rigidbody.SweepTest which I think does something similar.
If you need random directions then you need to do the above with the calculation of the quaternion on each frame after getting the new random value.
Your answer
Follow this Question
Related Questions
Ray Casting - Trigger function 1 Answer
how detect "OnRaycastOut" 1 Answer
set ray only when raycast a specific layer 1 Answer
Is there a way to detect if an object is being hit by a raycast? 2 Answers
Raycast and RaycastHit failure 2 Answers