Search Unity

Raycasts don't work against some objects' colliders

Discussion in 'Physics' started by PanicEnsues, Dec 24, 2020.

  1. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    187
    I've seen one or two mentions of this in other posts, but nothing concrete: Raycasts don't work against some objects' colliders.

    Symptoms are:
    1. Raycasts against certain instanced objects' colliders just pass right through them every frame.
    2. Collisions with/between those objects work fine.
    3. Other instances of the same prefab work fine.
    4. AddForce to a broken object's rigidbody works fine.
    5. A broken object's Rigidbody Sleep State shows as "Awake".
    6. Total scene collider count ranges from 700 to 1600.
    7. Happens whether objects are moving or stationary.
    Things I've tried that "fix" it:
    1. Unparent/reparent collider to object: fixes it for that one collider.
    2. Disable/Enable collider gameobject: fixes it for that one collider.
    3. Remove Rigidbody from object: fixes it for all colliders on that object; can then re-add a new rigidbody, still works.
    4. Duplicate a non-working collider: the duplicate works (original still doesn't).
    Things I've tried that don't have any effect:
    1. Physics.SyncTransforms().
    2. Disable/Enable collider component.
    3. Calling Rigidbody.Sleep() / WakeUp().
    4. Changing collider's layer.
    5. Changing rigidbody's layer.
    6. Changing object's entire hierarchy layer.
    7. Changing length of raycast.
    BrokenRayCast01.jpg

    Can anyone shed any light on this?

    -Scott
     
  2. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    Have you checked the physics debugger? I had a similar problem where the position of some colliders weren’t synced anymore with their transforms after I set timeScale to zero. Makes some sense in my case because the physics engine doesn’t simulate any steps, but I was still very puzzled. Especially because the collider was shown in the correct orientation in the scene view without the physics debugger.

    Do you manually simulate the scene or do you set timeScale to zero?
     
  3. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    187
    Everything appears correct in the Physics debugger: All boxes are in the right place and right color (red for Rigidbodies).
    The colliders still work correctly for collisions (they hit each other), it's just raycasts that fail, whether the scene is running or at timescale 0.
     
  4. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    Maybe you’re using ArticulationBodies instead of Rigidbodies? I also had problems when parenting/unparenting ArticulationBodies from their hierarchies.. other than that I also don’t know :/
     
  5. Deleted User

    Deleted User

    Guest

    Check your object scaling. I had an issue where non-uniform scaled objects didn't get hit. Maybe just make everything scale 1.
     
  6. PanicEnsues

    PanicEnsues

    Joined:
    Jul 17, 2014
    Posts:
    187
    Good idea! But I just checked, and the colliders are all 1,1,1 scale, with their dimensions set via their Size property. They're children of a transform that is 2,2,2, but scaling that parent to 1,1,1 doesn't fix the issue.

    Thanks for the suggestion, let me know if you have any other ideas.