Search Unity

Raycast Detect the point “Out” of collider, not only just “In”

Discussion in 'Scripting' started by JayHuang, Jul 1, 2014.

  1. JayHuang

    JayHuang

    Joined:
    Jul 1, 2014
    Posts:
    4
    Hello,

    By using Physics.Raycast in Unity, we can get hit information easily with the point, normal, collider name... But how can I get the "point" where ray go out of the collider?

    Many thanks for your time thinking about this problem.
     
  2. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    That's a non-trivial question... There's a few things to consider here, primarily that the "in" point isn't necessarily the only "in" point. The returned point from a ray cast is the closest contact point. There many be many other contact points, each of which may be an "in" or an "out" (or neither or both, if we take abstract geometry into account).

    So, depending on what you want, I'd suggest making an opposing ray casting back towards the object and see where the first contact point on the far side is - which would be the same as the final contact point on that object if you'd continued the ray all the way though. That's assuming that's what you want, and not actually an array of contact points to do arbitrary stuff with.
     
  3. JayHuang

    JayHuang

    Joined:
    Jul 1, 2014
    Posts:
    4
    Hello AngryPenguin,


    Thanks a lot for the suggestion and experience. Really helpful!

    Although my target colliders are simple (deformed basic geometry), this issue you brought out is really interesting.

    I am not sure how best I can solve this issue using your suggestion, since there will be risk if the ray from another direction will hit on other targets/colliders.

    The offset where the ray start from another direction is a bit tricky.

    Still look forward to any possible solution...


    Best,
    Jay
     
  4. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,620
    You can ray cast against a single collider. From memory it's collider.Raycast(...).