Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Raycasting on a mesh collider

Discussion in 'Scripting' started by timoshaddix, Feb 19, 2016.

  1. timoshaddix

    timoshaddix

    Joined:
    Jan 7, 2014
    Posts:
    64
    hello,

    I am trying to cast a raycast onto a custom 3D model using a mesh collider.
    But the problem is the raycasting only works when the mesh collider is Convex.
    and i really can't use Convex.
    Is there a solution for this?

    Greetings.
     
  2. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    A RayCast will hit a mesh collider regardless if it is convex or not, as long as it is not set to isTrigger = true.
    Anyway, if it is indeed a trigger then you have to use a compound collider (multiple convex colliders on the same gameObject).
     
  3. timoshaddix

    timoshaddix

    Joined:
    Jan 7, 2014
    Posts:
    64
    That's what i thought too, but somehow it really won't hit.

    This is the raycast script:
    Code (CSharp):
    1.     void StayOnGround ()
    2.     {
    3.          int layerMask = 1 << 8;
    4.          RaycastHit hit;
    5.          if (Physics.Raycast(transform.position, -Vector2.up, out hit, Mathf.Infinity, layerMask))
    6.          {
    7.             transform.position = new Vector3(transform.position.x, hit.point.y, transform.position.z);
    8.          }
    9.     }
    And the screenshot of the scene:
    http://imgur.com/1b9v1sU

    I really don't know why it won't work.
    But setting it to convex works, so that is odd.
     
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,739
    non convex colliders are pretty expensive and not too reliable just use multple convex ones
     
  5. Cherno

    Cherno

    Joined:
    Apr 7, 2013
    Posts:
    515
    It could be a problem with flipped normals, as well.