Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 JohnnytheRocket · Jul 28, 2014 at 06:38 PM · raycastraycasthitcollide

Raycast collides on nothing?

Hi, I've googled pretty hard for anyone with my problem, but to no avail. So here goes:

My raycast collides on seemingly nothing. Raycasthit.distance returns "42.62..." and Raycasthit.collider returns "null".

 {
     RaycastHit hit;
     Physics.Raycast(transform.position, direction, out hit);
     
     Debug.DrawRay(transform.position, direction * hit.distance, Color.green, 4f, true);
 }

I've also tried increasing the raycast distance "manually" (even though that wouldn't make sense), with

 Physics.Raycast(transform.position, direction, out hit, mathf.infinity);

alt text

So yeah, what is going on?

Thanks for your time!

EDIT: It seems like I provided too little code. I've figured out that it apparently is relevant to mention that the raycast is made inside of a for loop:

 for(int i =0; i < x; i++)
 {
    for (int j = 0; j < y; j++)
    {
         RaycastHit hit;
     Physics.Raycast(transform.position, direction[i,j], out hit);
    }
 }

hit.distance works properly (i.e. is able to become infinitely large) as long as hit.distance never has been smaller earlier in the loop.

Example: Physics.Raycast(transform.position, direction[0,0], out hit) is always able to return infinty, whereas Physics.Raycast(transform.position, direction[x-1,y-1], out hit) almost never is (since the raycast is bound to hit something until then).

raycast.png (19.5 kB)
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by JohnnytheRocket · Jul 28, 2014 at 07:42 PM

I managed to find a solution by resetting hit.distance = 1000f; after reading it.

Doesn't make a lot of sense to me but at least it works...

Comment
Add comment · 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
1

Answer by rutter · Jul 28, 2014 at 06:41 PM

The contents of your RaycastHit are only valid if Physics.Raycast returned true (indicating that it hit something).

There may be some data present, regardless, but it's garbage if the raycast didn't actually hit anything.

Usual structure for a raycast is more like this:

 RaycastHit hit;
 if (Physics.Raycast(transform.position, direction, out hit)) {
     //hit something!
     Debug.DrawRay(transform.position, direction * hit.distance, Color.green, 4f, true);
 } else {
     //didn't hit anything
     Debug.DrawRay(transform.position, direction * 1000f, Color.red, 0.1f, true);
 }
 


Comment
Add comment · Show 3 · 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 JohnnytheRocket · Jul 28, 2014 at 07:09 PM 0
Share

Thanks for the speedy answer!

I've edited the question as I didn't provide enough information the first time around.

With that said I opted for my current code as the entire grid of raycasts is a lot more understandable if all of them are drawn.

avatar image rutter · Jul 28, 2014 at 07:14 PM 0
Share

I see the loop, but it doesn't change my advice. The RaycastHit data is only valid if the call returned true. If you're not checking the return value, you're likely to encounter errors.

avatar image JohnnytheRocket · Jul 28, 2014 at 07:22 PM 0
Share

Alright, will do so.

avatar image
0

Answer by matthew2 · Mar 26, 2017 at 05:53 PM

Although this is late, and this is an unorthodox solution, disable "queries hit triggers" in the physics menu, under "Edit -> Project Settings"

Comment
Add comment · 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

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

Raycast Not Working 3 Answers

Find gameobjects in a crosshair? 1 Answer

Raycast won't hit Collider 1 Answer

Not getting right value; ScreenPointToRay 0 Answers

help the build button in the build settings is grayed out 0 Answers


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