Search Unity

Animation breaks Physics.Raycast?

Discussion in 'Editor & General Support' started by DGordon, Sep 2, 2014.

  1. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    [I posted this in a different section a few days ago, but I haven't gotten a single reply. If this isn't the proper place, please point me in the right direction, thanks!]

    Hey,

    I'm trying to do a very simple thing (I think its simple?) of attaching colliders to a characters bone structure so they move as he moves. However, when I play an animation (using mechanim), raycasts stop working properly against those colliders. From what I read, this has to do with FixedUpdate and physics, and setting the animator.updateMode to AnimatePhysics is supposed to fix this. However, even when I set updateMode to AnimatePhysics, my raycasts are going straight through any colliders that have moved because of the animation. I've spent a whole lot of hours trying everything I can think of, surfing the web, tracing my raycasts, etc etc ... I can't figure this one out. I've done the whole bit about setting it to AnimatePhysics through code and not through the editor (as one post said), etc etc ... I'm at the point of trying strange things!

    All I'm trying to do is have discreet hitboxes on each limb for a FPS. Think fallout ... if you hit a limb, it can damage it, have a different effect, etc. I never imagined just getting my raycasts to work right was going to be the dealbreaker here ... the rest of everything is coming alone fine since I actually understand whats going on.

    Anyway, I would *really* appreciate some help here. I posted this else where and I haven't had any replies for days now, and I can't move forward with this aspect of my project until this is resolved.

    Thanks!,
    Dale
     
  2. netvortex_dc

    netvortex_dc

    Joined:
    Jan 13, 2014
    Posts:
    126
    Yup! The mecanim controller doesn't work on animating physics, it's actually blending the "physics"/colliders into the right spot - so in the editor you see the collider at the correct position but on the physics engine it's not really there.

    Switch your rigidbodies to kinematic so they don't fall down and when you disable the controller to enter ragdoll mode switch them to useGravity again - that way the colliders will remain in the position where they should be in. Worked for me.
     
  3. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    Hey,

    First off, thank you for the reply ... its the first I've gotten in days, so much appreciated. I get the problem (the first paragraph you wrote), but not the second paragraph if its intended to solve things.

    My current setup is using Mechanim animations, with colliders attached to the bone structure. The majority of the colliders were created using the Rag Doll wizard, with a few others added by me (hands and feet). Only the Rag Doll colliders have rigid bodies. All rigid bodies are set to kinematic through the checkbox in the editor. This does not change until the model dies, at which point I disable the Animator and enable kinematic on the Rigid Body so the model enters Rag Doll mode.

    The issue is that any collider moved by the animation has its hit box out of sync with the physics engine. This is as you said. However, this applies to all colliders -- regardless of if it has a rigid body or not. As such, its not caused by the Rigid Body, and kinematic/not kinematic isn't relevant to the issue (although I do thank you for mentioning it, that would have saved me a bunch of time if I had that info about a week earlier :)).

    How do I get the colliders which have been moved by an Animator to be hit by raycasts? I understand the problem -- they aren't in sync with the physics engine. How am I supposed to go about this then? I'm simply trying to get collision boxes to follow the characters limbs, which can be hit by bullets. Plenty of games have this functionality ... I don't think I'm trying to figure out anything too crazy here. I'm just clearly not attempting it in the right way within Unity ... I just don't know what I'm missing though.

    Thanks again,
    Dale
     
  4. netvortex_dc

    netvortex_dc

    Joined:
    Jan 13, 2014
    Posts:
    126
    Well my solution was going through every single collider, adding a rigidbody to it if it had a character joint and setting it to kinematic. That should allow your raycasts to work again. Now when entering ragdoll mode just get all the children and switch them from iskinematic to usegravity.

    I'm using animator motion as well and was running into this issue a few weeks ago, i was getting nuts here so i totally understand your frustration with this. Should have seen my face when i shot raycasts in all directions seeing the image of those colliders on the ground while Unity showing me them being in perfect position.

    Basically, it's all colliders in the hierarchy of your animator. It uses a skeleton which should point you to the objectlist, it's basically all of those. Once it's kinematic it will work perfectly.
     
  5. avi9111

    avi9111

    Joined:
    Feb 24, 2016
    Posts:
    31
    yeah, i encounter on the same problem, but I still could not understand how to fix it
     
  6. avi9111

    avi9111

    Joined:
    Feb 24, 2016
    Posts:
    31
    thanks DGordon,
    " I'm simply trying to get collision boxes to follow the characters limbs",
    I did the some solution as this.
    but this solution had 2 problems which could not be fixed
    1.some ware physics when I try to add force on the model
    2.we could not hit hand, body, head in a simple way.

    I wondered if any other devloper should have a easy way to handle Physic & Ragdoll
    (my colleage created model with Ragdoll wizard and I try to disable animator, it still have the same problem, the raycast through the model)
     
  7. avi9111

    avi9111

    Joined:
    Feb 24, 2016
    Posts:
    31

    I have to add a collider inside and pull the size to cover the whole model (as tall as it), the raycast issue fixed but it still some physic problem without expect.