Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Jiry_XD · Apr 10, 2021 at 03:19 PM · raycasttriggerraycasthittriggersraycasts

Raycasts not working on triggers.

My raycasts don't work on triggers, however I have "Queries hit triggers" turned on in physics settings, I have also tried QueryTriggerInteraction.Collide, but that does't work. However if I set my triggers to colliders it works, but I need them as a trigger, If I put them as a collider it breaks my game....(edited) this is an example of my code:

 if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out hit, 2f, laneTrigger, QueryTriggerInteraction.Collide) && isMoving && laneNum == 2 || Physics.Raycast(transform.position, transform.TransformDirection(Vector3.right), out hit, 2f, laneTrigger, QueryTriggerInteraction.Collide) && isMoving && laneNum == 0)
         {
             Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.right) * hit.distance, Color.yellow);
             Debug.Log("Right Hit" + hit.collider);
             moveX = 0f;
             isMoving = false;
         }
         else
         {
             Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.right) * 2f, Color.white);
             Debug.Log("Target not hit");
  
         }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by AndBje · Apr 10, 2021 at 07:44 PM

Are you sure you are only changing the "isTrigger" property on the collider? It sounds like there is something else that is causing the problems. The "QueryTriggerInteraction.Collide", which you have already tried, should make the ray hit whether it is a collider or a trigger (regardless of the physics settings).

When the ray misses - is the white debug line going through the object with the trigger?

  1. A common error I have seen is that the collider is disabled (I have never done so myself, of course). An easy mistake in the editor, but it can also be disabled by code. Check it while the game is running. alt text

  2. Make sure your ray isn't hitting the collider/trigger on the object you are pointing from.

  3. I guess you have already checked this - but is the "Layer Collision Matrix" in Project Settings correct?

  4. Double check the layer mask "laneTrigger" so it is set as you expect.

  5. If the raycast still does not hit - try and make a simple raycast call (without unnecessary game logic) and the layer mask set to Physics.AllLayers.

          if (Physics.Raycast(transform.position, target.position - transform.position, out RaycastHit raycastHit, float.MaxValue, layerMask, QueryTriggerInteraction.Collide))
             Debug.Log("HIT!");
         else
             Debug.Log("MISS... very strange!");
    
    
    

disabledcollider.png (13.1 kB)
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
avatar image
0

Answer by XSpitFire · Jul 19, 2021 at 08:45 PM

I also found that starting your raycast within a collider will not detect that collider. Make sure your raycast starts well outside of the collider

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

183 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 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 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 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 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 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 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 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

Raycast returns null for no apparent reason 0 Answers

Trying to play other objects' sound and animations from Raycast hit 1 Answer

[help]RayCast check [SOLVED] 2 Answers

ScreenPointToRay ray not coming through from camera 0 Answers

Raycast not working 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