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 /
avatar image
8
Question by Dreamer · Jun 14, 2011 at 01:41 AM · collisionraycast

Collision Detection If Raycast Source Is inside A Collider?

It seems that when you raycast 1 ray, if the origin point is inside the collider A, it won't able to detect collision with A. Is my observation correct?

If so, how to overcome it?

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
2
Best Answer

Answer by Bunny83 · Jun 14, 2011 at 01:52 AM

Yes this is right.

"Note: This function will return false if you cast a ray from inside a sphere to the outside; this in an intended behaviour."

See Physics.Raycast.

There is no general approach to circumvent this, it depends on what you want to achieve.

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
24

Answer by kimag · Sep 28, 2012 at 01:59 PM

You can reverse your ray:

 ray.origin = ray.GetPoint(100);
 ray.direction = -ray.direction;

Example from my project:

 var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 var hit : RaycastHit;
 ray.origin = ray.GetPoint(100);
 ray.direction = -ray.direction;
 if (theCollider.Raycast (ray, hit, 100.0))
 {
    Debug.DrawLine (transform.position, hit.point, Color.red);
 }
Comment
Add comment · Show 5 · 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 progmeer · Jul 05, 2014 at 06:28 PM 0
Share

Reversing is very elegant and solved the same issue I was having.

avatar image antonsavov · Feb 12, 2015 at 04:27 PM 0
Share

great tip ! thanks

avatar image batesasho · Oct 12, 2016 at 10:39 AM 0
Share

The most elegant way to reverse a raycast inside a collider (sphere in my case). Thanks, you saved my day!

p.s I wanted to place a empty game object outside the collider an point the raycast in collider direction, but the reverse raycast method is brilliant!

avatar image Duy_Tr · Aug 31, 2017 at 04:07 AM 0
Share

It awsome worked for me. Thanks!!

avatar image ENDAS_ORIGINAL · Jul 10, 2021 at 01:42 AM 0
Share

had to login so that I can upvote your answer :D

avatar image
16

Answer by computas · Apr 17, 2019 at 08:38 PM

Old question, but still first in google search. So here we go:

Unity now days have this option "Queries hit backfaces". It solved the problem for me. You can find it in "Edit/project setting/physics" alt text


backface.png (74.8 kB)
Comment
Add comment · Show 2 · 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 HakJak · May 24, 2019 at 01:20 PM 1
Share

This only works with mesh colliders though.

avatar image EzraHuffman · Apr 25, 2021 at 02:03 AM 0
Share

Thank you!

avatar image
1

Answer by ShawnFeatherly · Jun 17, 2013 at 02:46 AM

I needed to do this as well. kimag got me going in the right direction, but I needed a bit more accuracy.

I only had to deal with one GameObject being able to collide with the rays. Which made this a bit easier. My method was to use kimag method to not only find the collision point in front of the line but also behind the line. This results in what could be thought of as a line segment parallel and along the rays direction that runs along the object being collided with. After you have this, the rays origin is checked to see if it's inside that line segment.

 RaycastHit hit;
 Ray rayForward = new Ray(origin, end3 - origin);
 Ray rayBehind = new Ray(end3, origin - end3);
     
 //find point in front of ray
 rayBehind.origin = rayBehind.GetPoint(-100);
 Physics.Raycast(rayBehind, out hit, 100f);
 Vector3 beyondForward = hit.point;
     
 //find point behind ray
 rayForward.origin = rayForward.GetPoint(-100);
 Physics.Raycast(rayForward, out hit, 100f);
 Vector3 beyondBehind = hit.point;
 
 //check if rays origin is between points
 bool isRayInside =
        origin.x > Mathf.Min(beyondForward.x, beyondBehind.x)
     && origin.x < Mathf.Max(beyondForward.x, beyondBehind.x)
     && origin.y > Mathf.Min(beyondForward.y, beyondBehind.y)
     && origin.y < Mathf.Max(beyondForward.y, beyondBehind.y);
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

10 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

Related Questions

Receiving information about getting hit by a Raycast 2 Answers

Surface with hole and Raycast - Which collider 1 Answer

How to do a raycast to make my player unable to move through certain objects? 2 Answers

How do you raycast from an object ignoring only that object? 0 Answers

Detect if ball is on a platform or if it fell 4 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