• 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
Question by Rioneer · 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

People who like this

0 Show 0
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

  • Sort: 
avatar image

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

People who like this

0 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 Rioneer · 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 Rioneer · 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 Rioneer · Mar 11, 2014 at 06:01 PM 0
Share

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

Show more comments

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Cast multiple rays from an object between two angles? 0 Answers

Set raycast on player camera or object? 0 Answers

What's wrong with my RaycastHit2D? 1 Answer

Multiple Cars not working 1 Answer

(C#) Collider to collider2d 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