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 Andy-Block · Jan 29, 2013 at 10:51 AM · physicsraycastcolliderphysics.raycast

Why does my Physics.Raycast not hit my BoxCollider?

I have a panel in my UI. It is actually a box, which is a subcomponent of a prefab, which I instantiate from script at a position in front of the camera (and under it hierarchically). The box has a BoxCollider component. I can see in the scene view that the collider matches the box itself. When the screen is tapped, I'm doing this:

         var ray = Camera.main.ScreenPointToRay (screenPos);
         RaycastHit hit;
 
         print ("InfluenceEnvironment: check hit");
         if (Physics.Raycast (ray, out hit, 500f)) // Note that the panel's distance from the camera is less than 500, and I have tried specifying no distance and thus casting an infinite ray, but the effect is identical.
         {
             print (string.Format("InfluenceEnvironment: Got hit: {0}", hit.transform.gameObject.name));
             Debug.DrawRay(ray.origin, ray.direction*2000, Color.magenta, 10);
                         ...
         }
 

If I touch the middle of my panel, all is fine. But if I click the edge, it doesn't work. I can see in the scene view that the ray from Debug.DrawRay() is passing through the collider in question. Yet the object in the RaycastHit is the terrain, which lies behind the box. And, bizarrely, if I change the collider at all in the inspector (e.g. changing x pos by 0.0001), it starts working (but making the equivalent change from code does not help). Simply disabling / re-enabling the collider also has this effect of fixing it. I've tried changing to a capsule collider instead, just to see it that works, but also have the same problem.

I also see that if I add the prefab to the scene manually instead of in script, that also works.

I wonder if the problem is related to the one discussed in http://forum.unity3d.com/threads/572...-objects/page2. However, I don't think so, as it persists even when none of the objects involved nor the camera move.

I am using Unity 4.0.1f2

Any thoughts / workaround would be welcome! Regards,

Andy

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

4 Replies

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

Answer by Andy-Block · Feb 05, 2013 at 12:23 PM

I found a workaround for this. Immediately after creating my panel, I do:

         textPanelObj.SetActive(false);
         textPanelObj.SetActive(true);

Previously I had tried disabling / enabling the collider associated with the object, but not the object itself. This seems to resolve the problem.

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 DevLurkin · Jan 15, 2015 at 05:32 AM 0
Share

Thank you for this. After changing a collider's size at runtime, a raycast wasn't hitting it until the next frame, which is unacceptable for my circumstances. Deactivating and reactivating it like this fixed the issue.

avatar image
1

Answer by Qvintusdk · Jan 29, 2013 at 01:46 PM

Simply disabling / re-enabling the collider also has this effect of fixing it.

Isn't this your answer? If that fixes your problem why not grab the collider component and re-enable it upon instantiating it? It could simply be a bug with unity when instantiating object, that needs to be fixed and up untill then you need to use this workaround?

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 Andy-Block · Jan 29, 2013 at 01:49 PM 0
Share

No, I don't think this helps. As I mentioned, the prefab is created from script, so I can't disable it in the editor, and although I didn't mention it, I've tried enabling / re-enabling from script already (just as I have tried adjusting the transform from script), and that doesn't help - it only works if you change it in the inspector when running in the editor. Thanks anyway for the suggestion.

avatar image Qvintusdk · Jan 31, 2013 at 09:59 AM 0
Share

you might be able to use the script from this question: http://answers.unity3d.com/questions/390571/odd-collision-behaviour.html

avatar image Andy-Block · Jan 31, 2013 at 10:05 AM 0
Share

Hmm - interesting, sounds like it could be the same issue, indeed. I'm not in a state to test this right now, but will check it out when I can - thanks for the pointer!

avatar image Andy-Block · Feb 05, 2013 at 09:23 AM 0
Share

I did get a chance to try this, but it didn't help, I'm afraid. Thanks for the suggestion, though! Anyway, I did find a workaround, which I'll add as an answer.

avatar image
0

Answer by Owen-Reynolds · Jan 29, 2013 at 03:48 PM

If this is really a "UI box" which is being moved around to always occupy the same area on the screen, could just check screen coords directly, with no ray cast.

But, I'm wondering if the box is very close to the camera, and the rayCast is starting inside the box, so is ignoring it. ScreenPoint to ray starts NearPlane away from the camera (can print ray.origin.) If that was the case, could adjust the ray to start further back: ray.origin -= ray.direction * 2;

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 Andy-Block · Jan 29, 2013 at 06:52 PM 0
Share

Hmm - I don't think that's the problem; as I mentioned, Debug.DrawRay does show the ray going straight through the box. Also, if that was the issue, I would not have expected the disable / enable to have the effect of fixing it. Your point about just directly checking the screen coordinates is a good one, though. I'm not program$$anonymous$$g them directly in screen coordinates at the moment, but could work out a transform to allow me to check like that. However, I'd prefer to understand / work around the problem here differently, and rely on Unity to do the heavy lifting for me if possible. Thanks for your thoughts!

avatar image
0

Answer by hubecube_ · Nov 12, 2015 at 12:14 PM

I am having the same issue in Unity 5.0.0f4

I have a moving platform that the player jumps on. The player object casts a ray to see if it has landed on something.

  1. When the platform is placed in the scene, in the inspector there are no problem. The player lands and the raycast reports landing.

  2. When the platform is instantiated through script the rays pass right through it.

  3. Resetting (disable / enable) the game object or the collider did not help me.

  4. I found that if I move the platform down 0.04f that it again begins to get hit by the raycasts

The solution to my problem was to add a script to the platform's Start() AND I had to constrain the y-axis movement on the rigidbody.

         Vector3 posReset = this.transform.localPosition;
         posReset.y -= 0.04f;
         this.transform.localPosition = posReset;



Comment
Add comment · 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

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

13 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

Related Questions

Slant Physics Raycast Implementation 0 Answers

Changing object scale without changing collider scale 1 Answer

AI Players Movement for Rolling Ball Game 0 Answers

Determine object hitting a static collider at update() 1 Answer

methods for mouse picking that do not use physics? 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