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.