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 BigED · Oct 06, 2014 at 08:05 PM · raycastcolliderray

Rays: why is not working?

I have a terrain. It's size 500x500 with center in (0,0).

I want to create ray, but sometimes I have null reference exception, because my ray not colliding to any objects.

My code:

                     pos = new Vector3(Random.Range(-200f, 200f), 400f, Random.Range(-200f, 200f));
                     Ray ray = new Ray(pos, Vector3.down); 
     
                     Physics.Raycast(ray, out hit, 50000);
     
                     if (hit.collider.name == "Terrain") //sometimes null reference exception
                     {
                         //do actions
                     }
 


I see what Ray.origin is not same as "pos" vector. It is normal?

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

1 Reply

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

Answer by robertbu · Oct 06, 2014 at 08:27 PM

I'm unaware of any 'center' associated with a terrain. If you are talking about 'position', then terrains are achored in the corner. Create a terrain at (0,0,0), then create a sphere at (0,0,0). You will see the sphere in the corner of the terrain. So you will need to either offset the position of your terrain, or offset the 'center' position for your 'pos' used in the Raycast().

But beyond that, it is bad coding to do a Raycast() and not check the return. Structure your code:

 if (Physics.Raycast(ray out hit, 5000)) {
     // Do something
 }
 else {
     // Report the error
 }

Unexpected things happen in code, and you don't want your app to crash on the device.

Comment
Add comment · Show 4 · 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 BigED · Oct 07, 2014 at 02:32 PM 0
Share

Yes, I will create error reporting later, but my terrain locate at (-250, 0, -250)

avatar image robertbu · Oct 07, 2014 at 03:16 PM 0
Share

Well if you are getting a null reference exception, you need to put your error reporting in now. You could put this in the 'else'

     Debug.DrawRay(ray.origin, ray.direction, Color.red, 10.0f);

Then you could look at the raycast that failed to see what is going on.

avatar image Tourist · Oct 07, 2014 at 03:26 PM 0
Share

it is not about error reporting, it is about error handling. You should start by architecturing your code as requested above to better comprehend what happens.

avatar image BigED · Oct 08, 2014 at 04:07 AM 0
Share

Thanks to all!

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

2 People are following this question.

avatar image avatar image

Related Questions

Creating a working meshcollider in code 3 Answers

Raycast isnt working 1 Answer

Flattening terrain under my object at start() 1 Answer

why are child colliders sometimes ignored? 2 Answers

Someone can help with Ray/Collide? 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