Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by SuperRaed · Jun 08, 2016 at 06:27 AM · raycastlinerendererreflectionwhile-loop

is my Raycast being reflected correctly and why is it giving me an infinite loop?

I'm trying to build a light source that emits light from it's position and to the forward direction, which is visualized using a Line Renderer, and using RayCast() I want to detect weather or not it collides with an object and if so I want to reflect the light and on it goes

my implementation so far, which is causing me an infinite while loop

 void Update () {
 
         //for debugging purposes count how many reflections I get
         loopCount = 0;
         //a Rays object simply hold two Vector3 values, a starting position and a direction
         Rays ray = new Rays(startPos.position, startPos.forward);//
 
 
         while (Physics.Raycast(ray.rayPosition, ray.rayDirection, out hit, distance)) {
             
             //in case it hits then I want to store the position of the collider and its direction
             //to be used in the next Raycast itteration
             Vector3 reflection = Vector3.Reflect(ray.rayDirection, hit.normal);
             ray = new Rays(hit.point, reflection);
             
             loopCount++;
             
         }
         Debug.Log(loopCount);


I can't figure out why I'm getting an infinite loop , unless my new rays are reflecting in correctly so that I'm always getting a true value from my Raycast()

Comment
Add comment · Show 2
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 tanoshimi · Jun 08, 2016 at 06:38 AM 2
Share

Well, to debug I'd start by changing your while (...) to a while (... && loopCount < 10) and them output the value of ray on each iteration to see if it's what you expected.

avatar image SuperRaed tanoshimi · Jun 08, 2016 at 06:49 AM 0
Share

thank you! I know why I was getting an infinite loop... apparently the object that I was reflecting my ray against was parallel to my the light source so the reflected light would bounce back to the lightsource and would go bouncing back and forth...I guess maybe I will either disable my lightsource collider or perhaps use a Layermask for more specificity

0 Replies

· Add your reply
  • Sort: 

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

60 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why is the reflected ray the same length as the previous and how to fix it 1 Answer

Raycast Reflection 2D 1 Answer

2D How to reflect a raycast with a line renderer 0 Answers

Having an issue Making a second ray cast with a new angle 1 Answer

Setting positions of points on Line renderer to mouse position does not work 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