Search Unity

How can i raytrace the thickness of a object?

Discussion in 'Scripting' started by Nanako, Nov 23, 2014.

  1. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    I raytrace in a direction, and it hits a wall. the raytrace keeps going, and at some point exits the wall again.

    How can i find the length of the part of the raytrace which is inside the wall?
     
  2. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Wouldn't that just be the thickness of the wall that you can grab from the mesh/collider?
     
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Short of storing the thickness of the wall, you can't without an opposing raycast, the data isn't available.
     
  4. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    976
    Not sure if feasible in this situation, but you could raycast against layermask, and set up mesh colliders for in and out wall. At hitpoint of the in-wall, create new raycast in same direction. This ray will hit wall 2. The difference of hitpoint 1 and 2 is the thickness of that trajectory.

    Quicker and easier would be to simply store approximate thickness in variable.

    edit:
    Silly me, if using 2 rays it's clearly better to just create a raycast from other side to get the 2nd hitpoint as hippocoder suggested..
     
    Last edited: Nov 23, 2014
  5. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    Yes, assuming i only want the exact thickness based on a perpendicular raycast.

    However if, for instance, i hit the wall at an angle, it's going to go through more than just the thickness of the wall, but also along some of it's length., angles are important in this case.

    Especially, too, for objects which aren't completely regular walls. Like casting through a sphere (and not hitting the centre)
     
  6. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    Is there any way to raycast to hit only a specific object? eg; that wall?
     
  7. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Layer mask.
     
  8. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    Or, cheap and nasty - collect the hits (RaycastAll) and then iterate through them looking for something specific (i.e. that wall).
     
  9. Deleted User

    Deleted User

    Guest

    you can use collider.RayCast() wich will only cast against this collider. Somethink along these lines

    Code (csharp):
    1.  
    2. if( Phyics.Raycast( ..., out firstHit) )
    3. {
    4.     if( firstHit.collider.Raycast( oppoiteDirectionOfFirstRaycast, out secondHit )
    5.     {
    6.         var colliderThickness = Vector3.Distance( firstHit.point, secondHit.point );
    7.     }
    8. }
    9.  
     
    Gekigengar, Nanako and lordofduct like this.
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,531
    Yeah, I'd go with what element_wsc says.

    That second raycast on the collider, with the opposite direction ray, is going to need a origin far away from the hit point.

    Issue, if it hit at a very acute angle, the distance through the solid could be very long, especially if it's a very long wall or something. Imagine a cube scaled out several thousand units (for whatever reason) and you hit at 1 degree off the surface, that ray could easily travel the whole length of the cube.

    Best option is to check the 'bounds' of the collider. Then select a point on the first ray that is outside the opposite side of the collider's bounds, and cast backward from there.
     
    david_arel likes this.
  11. jvhgamer

    jvhgamer

    Joined:
    Jun 27, 2017
    Posts:
    36
    Figured it out.
     
    Last edited: Aug 11, 2017