• 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 SimonDiamond · May 02, 2013 at 10:27 PM · c#physicsraycastworkdrawray

Raycast doesn't work properly?

So my camera throws a Raycast forward, and if this raycasts target hits a specified target, i want it to tell me. Very simple. Altough, it doesn't seem to always tell me when the target gets hit even though i can clearly see it does get hit. I do use Debug.DrawRay so there shouldn't be any problem.

I do have this theory that i need to hit the exact pivot point or middle point of the object, but that shouldn't be neccesary. Did i miss something here?

Here's the code.

 Debug.DrawRay(transform.position, transform.forward * 100f, Color.green, 20); 
             if(Physics.Raycast(transform.position, transform.forward, out Target, PlayerReachRange) && Target.transform.tag == "MovableObject")
             {
                 Debug.Log ("Shiggidiboo");        
             }
Comment
Add comment · Show 8
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 Waz · May 02, 2013 at 10:29 PM 0
Share

Note that raycasts only hit Colliders on the object. Is your collider big enough?

avatar image Dave-Carlile · May 02, 2013 at 10:31 PM 0
Share

What is the value of PlayerReachRange? Have you logged to to be absolutely sure? Are you sure, and have you verified with code, that the target tag is $$anonymous$$ovableObject?

avatar image SimonDiamond · May 02, 2013 at 10:38 PM 0
Share

The Collider covers the whole gameobject i'm trying to access. The value or PlayerReachRange is about 5 feet forward, and i know it is because sometimes but only somtimes it tells me it hits it. And i'm positive that $$anonymous$$ovableObject is the target tag. I've doubled checked. Gonna go do it again :)

avatar image SimonDiamond · May 02, 2013 at 10:55 PM 0
Share

The ray doesn't need to end inside the collider right? Because it feels like i'm missing something. Are you people having similar problems?

avatar image Waz · May 02, 2013 at 11:52 PM 0
Share

The ray needs to reach the collider. It can go into it or past it. It must not stop short. With floats, it can't just reach the surface of the collider either.

avatar image Waz · May 02, 2013 at 11:53 PM 0
Share

Note that if it hits something before the target that does not have the tag you test for, the if will also fail - to overcome this, RaycastAll and loop through the results looking for the closest one with the desired tag (note that the results are not sorted, IIRC).

avatar image AlucardJay · May 03, 2013 at 02:00 AM 1
Share

You should really use the PlayerReachRange variable in your draw ray :

 Debug.DrawRay(transform.position, transform.forward * PlayerReachRange, Color.green, 20);

Also, split up your conditionals and do a debug to see what the ray hit :

 Debug.DrawRay(transform.position, transform.forward * PlayerReachRange, Color.green, 20);
 if(Physics.Raycast(transform.position, transform.forward, out Target, PlayerReachRange) )
 {
     Debug.Log ("Ray Hit " + Target.transform.tag);
     if(Target.transform.tag == "$$anonymous$$ovableObject")
     {
         Debug.Log ("Shiggidiboo");
     }
 }
avatar image SimonDiamond · May 03, 2013 at 07:38 PM 0
Share

Thanks for all the help guys! I will take what you have suggested and see if i can try to explain what caused my problem. I'll get back to you.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Make Ray hit own collider 1 Answer

Balancing Forces + Gravity 1 Answer

Held cube clips through wall 0 Answers

Raycast doesnt detect object in front of rigidbody (player is stuck on wall) 1 Answer

Physics2D.Raycasting questions 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