Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 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 /
  • Help Room /
avatar image
0
Question by xthunderduckx · Jun 11, 2019 at 11:25 PM · physicsbugraycastingcamera.main

Physics.Raycast returns odd results when collider is not visible to camera

Edit: I also discovered that the results are the same with Physics.linecast. I fear unless there is a way to make the raycast work properly when the collision point is not visible to the main camera then raycastAll is the only option I have.
Edit 2: I also just found out that sendMessage has no receiver when using a simplified version of the code. I imagine if I tried to sendMessage(takeDamage) at any point on the objects which are not visible, then the same error would be thrown. In other words, the raycastHit.collider.gameObject is not a receiver? I have no clue.


It seems that raycasts return weird results whenever the object the ray is colliding with is not visible to the main camera.

 for (int i = 0; i < collisionPoints.Length; i++)
 {
     Ray explosionRay = new Ray(transform.position, collisionPoints[i].transform.position - transform.position);
     float maxDistance = Vector3.Distance(collisionPoints[i].gameObject.transform.position, transform.position);
     //RaycastHit[] explosionHits = Physics.RaycastAll(explosionRay, 1.5f);
     bool targetReached = false;
     while (!targetReached && Physics.Raycast(explosionRay, out RaycastHit explosionHit, 1.5f))
     {
         print(explosionHit.distance);
         if (explosionHit.collider.gameObject == collisionPoints[i].gameObject)
         {
             explosionHit.collider.gameObject.SendMessage("takeDamage", 225);
             targetReached = true;
         }
         else
         {
             if (explosionHit.collider.gameObject.tag != "Enemy")
             {
                 targetReached = true;
                 print("Hit a " + explosionHit.collider.gameObject.tag.ToString());
             }
             else
             {
                 //raycast again.  distance decreases by previous raycast's collision point distance from origin.  
                 targetReached = true;
             }
         }
     }
 }

Physics.RaycastAll seems too finnicky, returning odd results such as the same hit twice in an array, as well as requiring a sorting algorithm to properly utilize. With physics.raycast in a loop, I'm not using an array and I know the hits will come back in order. Unfortunately, based off extensive testing, the above code's raycasts actually don't hit anything(?) when the object's supposed collision point is not visible.

alt text In this image, the projectile lands centered in the middle of all five objects, and since the faces with which the raycast are colliding with are visible to the camera, the code works fine.
alt text In this image, the projectile lands in the same spot, though the main camera can not see the faces of two of the cubes, and as a result, I get the weird "hit a untagged" message in console. The cubes are all tagged as "Enemy" and their layer is "damageable", so I don't know why this would be occuring.

I've been stuck trying to make these explosions work for an eternity; How can I make it so the raycasts work properly even when the objects are not visible to the camera?

only3work.png (150.7 kB)
all5work.png (155.0 kB)
Comment
Add comment · Show 2
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 Owen-Reynolds · Jun 12, 2019 at 06:52 PM 0
Share

Didn't read the code, but off-camera raycasts work fine. Raycasts don't care about any camera stuff, only colliders. And camera rendering tricks won't change those.

Something else is probably funny. For example, another script is moving or de-activating stuff that goes off-camera. Or maybe you have some collider that tracks the camera, which is blocking raycasts from outside to inside.

Try printing more - like the name and position of what you hit. Or, just to see, pull/widen the camera so nothing is off-camera.

avatar image xthunderduckx Owen-Reynolds · Jun 12, 2019 at 07:10 PM 0
Share

Hmmm. Looks like the grenade itself, and all its individual parts, are being hit by the raycastAll.

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.

Follow this Question

Answers Answers and Comments

189 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Raycast is not hitting collider 2 Answers

moving in a grid (2D) 1 Answer

Adding MaxDistance to RayCast Stops Raycast From working 0 Answers

How to ensure raycasts hit colliders inside of trigger colliders on objects that have a rigidbody? 0 Answers

How to reflect raycast rays of objects 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