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 Nick4 · Mar 11, 2014 at 05:34 PM · c#raycastraycasting

Raycast function doesn't work.

     GameObject GetObject(Vector2 pos)
     {
         Ray ray = Camera.main.ScreenPointToRay(pos);
         RaycastHit hit;
 
         if(Physics.Raycast(ray, out hit))
         {
             return hit.transform.gameObject;
         }
 
         return null;
     }

I never had problems with raycasting before. This simple method always returns null even though there's always an object at given Vector2 position. I have colliders attached to my objects, in fact I'm using raycasting in other scripts and they work just fine. So what could be the problem?

Edit : Well, I ended up using OverlapSphere instead but that used almost a quarter of my cpu since I had to call it every frame. So I'm back to simple raycast. Here's more further info

 Ray ray = new Ray(new Vector3(1,2,0), Vector3.zero);
 print(ray.ToString());
 

What I get :

 Origin : (243.4, 104, 0)...

How's that even possible?

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
0

Answer by wibble82 · Mar 11, 2014 at 05:39 PM

Nothing obviously wrong with that code. Have you tried Debug.Log to print out the input pos, and ray information. Might point to some dodgy data. You can also use the debug rendering to draw the actual ray in the edit view, so you can confirm visually that it is intersecting the collider of a target object.

Also worth checking whether the colliders are set to 'trigger' - I'm not sure whether rays detect trigger intersections by default.

Comment
Add comment · Show 9 · 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 Nick4 · Mar 11, 2014 at 05:49 PM 0
Share

I tried them all. Everything seems fine and in place. It just doesn't pick up the object.

avatar image whydoidoit · Mar 11, 2014 at 05:51 PM 0
Share

The object has a collider and is not on the ignoreRaycast layer? Is the collider a mesh collider or a simple one?

avatar image Nick4 · Mar 11, 2014 at 05:55 PM 0
Share

Correct and it's a sphere collider.

avatar image whydoidoit · Mar 11, 2014 at 05:56 PM 0
Share

And you're sure pos is right? Debug.Logged it? It's in pixels?

avatar image Nick4 · Mar 11, 2014 at 06:01 PM 0
Share

100 percent sure. I even drew a ray to be sure.

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

23 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

Related Questions

Another Raycast Issue. 0 Answers

Problem with raycasting, checking for ground doesn't work 3 Answers

What's a reliable way to detect if an object is no longer being hit by a ray? 1 Answer

Raycasting ignoring small sized object 1 Answer

C# Raycast goes straight into the air 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