Search Unity

Raycast not working with mesh collider

Discussion in 'Editor & General Support' started by Pallongo, Sep 21, 2017.

  1. Pallongo

    Pallongo

    Joined:
    Sep 21, 2017
    Posts:
    2
    Hello Unity community,

    I' m working on my first Unity project and I can't solve the following issue: physics raycast is not working on a mesh collider. The raycast is between player and ground.

    - Player has a rigidbody attached and a sphere collider.
    - Ground has a rigidbody and a non-convex mesh collider.
    - Collision works perfectly but raycast detects nothing.
    - Maybe my "ignore raycast" configuration is wrong, I'm new to this.



    I've been looking for hours and can't find the solution.

    Thanks in advance,
    Pablo

    ------------------------------------
    EDIT: Just realize there's a special place for answers, going to post there, sorry.
     
    Last edited: Sep 21, 2017
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    The forums are just as good a place to ask for help as the answer page =)

    Have you set the "layers" on your objects? Unity will not automatically know what object the "Player" is, so you have to select the Player GameObject and set the Layer to "Player" in the upper-right of the inspector.

    On colliders, there's a checkbox for "IsTrigger". Physics raycasts usually don't hit triggers unless you tell it to with the "queryTriggerInteraction" parameter in the raycast function call.
     
  3. Pallongo

    Pallongo

    Joined:
    Sep 21, 2017
    Posts:
    2
    Thanks for the response,

    Both gameobjects have their corresponding layers and none of the colliders have "isTrigger" activated. After hours of trying to solve this I was thinking the problem has something to do with Tiled2Unity mesh. In order for the Player to collide with the mesh imported from Tiled, I have to use a sphere collider becase 2D colliders won't work. The collision works, however raycast never works. However, when I add a Box collider to the tilemap, raycast works inmediately. Could it be because the mesh has no Z size? I'm out of ideas.
     
  4. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    Ahh, your problem is that Physics.Raycast() only hits 3D colliders. To hit 2D colliders, use a Physics2D.Raycast()