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

[SOLVED] Can't raycast and hit triggers without having a rigidbody

Discussion in 'Scripting' started by Palimon, Oct 7, 2015.

  1. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Hey guys, having trouble with Raycasting. On my custom (non-physics) character controller I use trigger colliders to stick to the floor. I had a trigger box collider and kinematic Rigidbody on each floor collider, but I've been going through and cleaning up old code/objects (now that I'm older and wiser :)) and saw I didn't actually need the RB for the trigger collider. (At least, that's my current understanding.) So I removed the RB from my floor colliders. Now, my character falls through the floor. After realizing I needed to specify to the Raycast to hit triggers, I changed to the call which specifies trigger interaction and my new Raycast call looks like this:
    Code (CSharp):
    1. bool wasCollision = Physics.Raycast(transform.position, (-transform.up) + desiredMove, out downhitInfo, 20f, int.MaxValue, QueryTriggerInteraction.Collide);
    I'm still not hitting my trigger colliders, and if I put a kinematic RB on them, I do. What's up?

    An example of my floor collider:
    upload_2015-10-6_23-58-22.png
     
  2. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    I can't count the times people have argued that and told me it wasn't possible.
    The docs even say a rigidbody or a character controller
     
  3. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    at least one thing involved in the collision needs to have a rigidbody, if neither do the physics engine doesn't know about them, so how is it supposed to say "hey a collision happened"...?
     
  5. Palimon

    Palimon

    Joined:
    Apr 18, 2013
    Posts:
    225
    Ahh, I obviously thought triggers completely removed the need for RBs. Must have stopped reading after the second sentence in that ref page, and missed the third sentence :). Thanks! I was so confused!