Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Raycast not hitting ProBuilder Mesh Collider

Discussion in 'Editor & General Support' started by BlakeGillman, Jul 17, 2018.

  1. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
    Hours of googling to no avail.
    I have a level imported in and I've checked "Generate Colliders". This means I'm using a Mesh Collider (NON-Convex) and I can't use any other type of collider. And when I use my raycast it doesn't detect the level as existing.

    EDIT:
    After narrowing further I've discovered that it's ProBuilder. My level mesh was generated using ProBuilder and I've checked "Generate Colliders" but still the ray passes through it. After testing normal mesh collider's the ray does indeed hit them

    Code (CSharp):
    1.  
    2. public Transform point;
    3. Ray ray = new Ray(point.position, point.forward);
    4.             Transform hitTransform;
    5.             Vector3 hitPoint;
    6.  
    7. hitTransform = FindClosestHitObject(ray, out hitPoint);
    8.             if (hitTransform != null)
    9.             { // If we hit something
    10.                 Debug.Log("Hit: " + hitTransform.name);
    11.             }
    Code (CSharp):
    1. float maxRange = 10f;
    2. Transform FindClosestHitObject (Ray ray, out Vector3 hitPoint)
    3.     {
    4.         RaycastHit[] hits = Physics.RaycastAll(ray);
    5.  
    6.         Transform closestHit = null;
    7.         float distance = 0;
    8.         hitPoint = Vector3.zero;
    9.  
    10.         foreach (RaycastHit hit in hits)
    11.         {
    12.             // If this thing we hit is not us and we haven't hit anything else or this is closer than the last thing we calculated for
    13.             // and if this is within range to even hit whatsoever
    14.             if (Vector3.Distance(hit.transform.position, transform.position) <= maxRange &&
    15.                 hit.transform != this.transform &&
    16.                 (closestHit == null || hit.distance < distance))
    17.             {
    18.                 closestHit = hit.transform;
    19.                 distance = hit.distance;
    20.                 hitPoint = hit.point;
    21.             }
    22.         }
    23.  
    24.         return closestHit;
    25.     }
    Any and all help is appreciated as the bullets being able to hit the walls of the map is pretty important.
     
    Last edited: Jul 17, 2018
  2. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
  3. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
  4. BlakeGillman

    BlakeGillman

    Joined:
    Dec 7, 2013
    Posts:
    412
  5. kaarrrllll

    kaarrrllll

    Unity Technologies

    Joined:
    Aug 24, 2017
    Posts:
    552
    I don't understand, when you say imported the level do you mean that you're using the results of ProBuilder's "Export to Obj"?

    If I were to take a wild guess, it would be that you're casting the ray from inside the collider, which does not work.
     
    SarfaraazAlladin likes this.
  6. SarfaraazAlladin

    SarfaraazAlladin

    Joined:
    Dec 20, 2013
    Posts:
    280
    you just saved me. I was having this exact problem, but when I double checked, I was casting from just beneath the surface of the ground.