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
1
Question by Digital-Phantom · Mar 29, 2015 at 06:06 AM · raycastnullhitdebug.logregister

How to register if my Raycast hits nothing(Solved)

Sorry if I word this incorrectly, but how do I register if a raycast hit returns null ?

At the moment I'm simply using a crude method of having an invisible wall behind the player (lowerWall) and having the raycast register that if it hits nothing else.

 if(Physics.Raycast(shootingRay, out hit, attackDistance))
         {
             if(hit.collider.tag == "LowerWall")
             {
                 Debug.Log("Searching");
             }

             else

             if(hit.collider.tag == "ShieldCube")
             {
                 Debug.Log("Blocked");
                 canFire = false;
             }
 
             else
             
             if(hit.collider.tag == "Enemy")
             {
                 canFire = true;
                 InvaderFireWeapon();
             }
 
             if(hit.collider.tag == "Player")
             {
                 canFire = true;
                 InvaderFireWeapon();
             }
         }
 

Is there a way programmatically to check if a raycast doesn't actually hit anything

I tried -

 if(hit.collider == null)
              {
                 Debug.Log("Searching");
              }
 
 

This didn't throw up any errors, but it didn't actually do anything either.

Any help greatly appreciated.

???

Comment
Add comment · Show 1
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 Mikilo · Mar 29, 2015 at 06:21 AM 0
Share

Where did you put the if statement?

2 Replies

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

Answer by sumeeton · Mar 29, 2015 at 06:27 AM

Yes. If the raycast doesn't hit any collider, it returns false.

So, do it in the else statment. Simple!

 if(Physics.Raycast(shootingRay, out hit, attackDistance))
 {
        // executes if hits a collider
 }
 else
 {
       // executes if it doesnt hit any collider
 }
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 Digital-Phantom · Mar 29, 2015 at 12:16 PM 0
Share

Sorry for late reply. Anyway.. works fine now, many thanks

:)

avatar image
4

Answer by pietrovismara · Dec 03, 2016 at 09:39 AM

If no collider is hit, hit.collider will be null, so you can also check for it this way:

 if (hit.collider == null)
 {

 }
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 spuchals · Sep 07, 2019 at 12:49 PM 0
Share

First answer that actually considers the real problem. I was calling it from another script so I needed to check hit and the popular obvious answer was not a solution for me.

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

25 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

Related Questions

Finding when a Ray isn't hitting anything? 1 Answer

Hit distance Change light range 1 Answer

How to hit two object with one raycast? 2 Answers

Need help with Third person shooter 0 Answers

Help Playing "Sound Clip" on RayCastHit? (RayCast help?) 2 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