- Home /
Raycasting 1000 times
Hello Guys, I am working on a simulation program where I need to cast 1000 ray from an object in each frame. Is this possible to do it so fast in each frame and still the program must run smoothly or even 100 milliseconds for 1000 rays. I saw in a blog that instead of using Physics.Raycast we can use collider.raycast to reduce the ray casting time since collider.raycast only casts 1 ray.
this is what I am trying as an example but I will change the angle of each ray in my actual implementation.
using UnityEngine; using System.Collections;
public class RaycastTest : MonoBehaviour {
float time = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 fwd = transform.TransformDirection(Vector3.forward);
time = 0;//Time.deltaTime;
Ray ray = new Ray(transform.position,transform.forward);//Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Collider collider = this.collider;
for (int j = 0; j < 1; j++) {
print ("Time:"+time);
for (int i = 0; i < 1000; i++) {
print ("There is something in front of the object!");
if (collider.Raycast (ray,out hit, 50.0f)) {
}
Debug.DrawLine(ray.origin, hit.point);
}
time += Time.deltaTime;
print("Time:"+time);
}
}
}
Any help is appreciated. Thanks in advance.
Answer by ravi_gohil999 · Aug 14, 2014 at 12:42 PM
Hey Guys, I have a simple Solution
for(int i = 0;i < 500;i++) { Physics.Raycast...... //Do not put any print or debug statements.Only the variables. Ray casting is very fast but the other statements take up lot of time. }
@rutter's answer should be accepted since it answers the title question and more generally explains what other users will find helpful when their search turns up this solution.
This accepted answer only addresses a very specific bug within the example code unrelated to the core question of speeding up raycasts.
Answer by rutter · Aug 01, 2014 at 09:36 AM
If you find yourself needing 1000 raycasts, there is probably a simpler way to do what you're doing.
Generally speaking, each raycast is something like O(log n)
complexity for colliders in the scene. They're well optimized to be very fast, but at some point you're going to slow down the game.
If you can consolidate some of those casts -- for example, into a spherecast -- that will be a much lighter load.
It's not unheard of to fire dozens or hundreds of raycasts per frame, but this can be one of those scenarios where a bit of effort toward optimization can be well worth it.
Agreed. Also, the notion that "Collider.Raycast only fires one ray" is, while technically true, not the reason it can be faster than Physics.Raycast. That latter also fires just one ray, it's just that Collider.Raycast automatically ignores all colliders except for the one used to call the function. You could achieve the same effect by using a layermask with Physics.Raycast, and have only that one collider be a member of the layer.
Ah, right! Good of you to point that out. What a strange function. I can imagine a few uses for it, but I've been blissfully unaware of it for years.
Thanks guys, I will definitely look into sphere cast and physics.raycast using layer mask but I want to jsut add something more. Well, there will be 1000 rays from a single object each ray will have a difference of 0.25 degree. from what I understand, does this mean that I can put a sphere collider around my object and find the distance of all the rays hitting by using hit.distance.
Thank you again.
Hi Guys, is it possible to use GPU or Compute shader to do this task. I found some documents stating that it can be done. Anybody has any idea about this.
I think I have a solution. I am still testing. I have created a for loop and in the for loop I removed all the print() ins$$anonymous$$d I just use operations like = and other. Don't print anything and speed seems to be good. Around few milliseconds. I will add some more lines of code and update everyone with the solution