Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ravi_gohil999 · Aug 01, 2014 at 09:32 AM · physicsraycastcolliderframerate

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.

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

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. }

Comment
Add comment · Show 1 · 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 gwelkind · Dec 04, 2021 at 08:49 PM 0
Share

@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.

avatar image
3

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.

Comment
Add comment · Show 6 · 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 CHPedersen · Aug 01, 2014 at 10:01 AM 0
Share

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.

avatar image rutter · Aug 01, 2014 at 10:06 AM 0
Share

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.

avatar image ravi_gohil999 · Aug 01, 2014 at 10:48 AM 0
Share

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.

avatar image ravi_gohil999 · Aug 14, 2014 at 03:04 AM 0
Share

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.

avatar image ravi_gohil999 · Aug 14, 2014 at 05:52 AM 0
Share

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

Show more comments

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

Passing values from Shader to C# script 0 Answers

Slant Physics Raycast Implementation 0 Answers

Determine object hitting a static collider at update() 1 Answer

Particular collider not working properly... 1 Answer

Changing object scale without changing collider scale 1 Answer


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