Search Unity

BoxCollider vs Physics.Raycast problem

Discussion in 'Scripting' started by Rav3n, May 9, 2013.

  1. Rav3n

    Rav3n

    Joined:
    May 9, 2013
    Posts:
    11
    Hi guys!

    I've got a problem, I'd like to ask your help about.

    I use the soldier character from Bootcamp. I've created box colliders for the different body parts. BoxColliders are parented under the appropiate bone GameObjects.

    I use Physics.Raycast for shooting. And the problem is, that there are some special cases when Raycast says there is no hit, but I'm clearly shoot across the collider. I used Debug.DrawRay() and it is seen even visually that the ray goes through the box collider.

    Usually it is working. So please don't give advices about layers, range, or so.

    The problem occurs only in special poses: eg. if enemy is crouching, looking sideways then I can't shoot him in the head.

    And here comes the interesting part: I suspected that the problem is related to parenting so I tried to move up the BoxCollider in the bone hierarchy (from head bone to eg. spine2). And TADAAAA...it's working.
    It is important that I aiming exactly to the same spot, and none of the characters moved away; I can make it work just by replacing the collider under a different bone. Under spine2 it is working, but under spine3 (and deeper) it is not.

    After that I thought the probelm may be is that the idle animation is moving / rotating the bones all the time. But I made a quick test script wich is moving and rotating a GameObject continuously, and the shooting is working if I make the collider the child of this moving object.

    My next guess was that I noticed that some of the bones has scale values like 0.9999997 instead of 1. But I made a test with this scale thing too and couldn't reproduce the issue.

    So has anybody any suggestion?
    Are there any known bugs with collider related to this issue?

    Thanks in advance
     
  2. Rav3n

    Rav3n

    Joined:
    May 9, 2013
    Posts:
    11
    Anybody???
     
  3. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    Have you debugged to see what collider is being hit from RayCastHit?
    It may be hitting a part of the solider that is not a collider or perhaps something else.

    HitInfo is your friend, just see what it is doing each time...
     
  4. Rav3n

    Rav3n

    Joined:
    May 9, 2013
    Posts:
    11
    Yes, I've debugged it. It hits the map behind the soldier.

    What bothers me is that it is clearly seen via Debug.DrawRay that the ray intersects the soldier's headcollider but the raycast says it is not.
    Used even RaycastAll() to Debug.Log() out all the hits, and it doesn't contain the soldiers headcollider.

    And just by moving up the collider in the bone hierarchy, the hit is approved. By moving it back to its place, raycast doesn't see it again.

    :(
     
  5. Rav3n

    Rav3n

    Joined:
    May 9, 2013
    Posts:
    11
    As I said it before the issue only appears in some specific situations.
    It seems as if it appears when tha angle between the ray and the side of the boxcollider is too low.
    If the ray is perpendicular (or close) with the colliders side the hit is approved.

    Does it make sense? Do you know such bug regarding boxcolliders?
     
  6. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,822
    I think I remember having this problem way back, and it turned out that Debug.DrawRay and RayCast were actually firing in different directions, even though I thought they were going in the same place. SO while the Debug.DrawRay showed one thing, the RayCast was actually going in another place.

    You may want to double-check that with some other tests. Sorry it's just an idea, I remember I had an issue with once.
     
  7. TheAMes

    TheAMes

    Joined:
    Jun 17, 2013
    Posts:
    23
    Did you ever solve this? I've got the same problem. Although I haven't tried moving the colliders up the hierarchy. I'm also using an idle animation and while it's playing I can't hit any of my colliders. If I disable the animator my character falls to the floor ragdoll style as expected and I can shoot them in the various parts successfully. But while the animator is active and playing the idle animation no hits are detected. Just a hit on the terrain behind him
     
  8. wang37921

    wang37921

    Joined:
    Aug 1, 2014
    Posts:
    102
    Same Problem, Unity Manual: If you move colliders from scripting or by animation, there needs to be at least one FixedUpdate executed so that the physics library can update it's data structures, before a Raycast will hit the collider at it's new position.
    But I don't know, how to do it.
    Anybody?