Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GameObject falls through terrain when applyRootMotion is off OR OnStateMove is overridden

Discussion in 'Animation' started by alexchesser, May 3, 2018.

  1. alexchesser

    alexchesser

    Joined:
    Sep 15, 2017
    Posts:
    147
    I've got an animation StateMachineBehavior where I've been working on "jump" functionality. (see this thread for more background)

    I've managed to figure out that turning off "applyRootMotion" while the object is jumping will allow for a really sweet-feeling jump to happen. This has opened up a small host of new issues. Primary among them can be summed up by "the object falls through the floor"

    I'm generally trying to follow the pattern seen in the StandardAssets user controller. That is
    1. on JumpKey set a flag in the AnimationController which triggers a transition to the jumping animation
    2. run a CheckGroundStatus while the user is in the air reset "IsGrounded" flag if the object intersects the ground
    3. IN AIR: ensure applyRootMotion is OFF, apply a force to the rigidbody in the direction & velocity of the object
    4. ON GROUND: ensure applyRootMotion is ON AnimationController returns to the multidirectional movement blend-tree
    I've found that jumping ends up falling through the floor (about knee-deep), executing a second jump (no button press), landing at "starting level" but with feet buried roughly -0.07 lower than the previous start point below the ground.

    That seems to prevent further jumps. (I assume those are prevented because of the way that CheckGroundStatus functions.)

    Now - for one thing I *currently* don't have any colliders on these objects (capsule, mesh or otherwise)
    Though I do have a rigidbody.

    I am using an AnimationController with a StateMachineBehavior and not a MonoBehavior which I don't think is critical - but means I am not using FixedUpdate like in the StandardAssets project. It does mean that all input code is happening within OnStateUpdate right now.

    I wonder - should I move away from using a StateMachineBehavior and into a MonoBehavior on the GameObject? I wonder if that would help.

    The statemachine behavior for controls was suggested during an official Unity training video (
    ). Maybe that was simplified advice for educational purposes.

    I've also found that even overriding OnStateMove will cause the GameObject to fall through the floor. Which ... strikes me as odd but I don't think it is really critical for fixing the jumping issue..

    Code (CSharp):
    1.  
    2. public override void OnStateMove(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
    3. {
    4.      base.OnStateMove(animator, stateInfo, layerIndex);
    5. }
    6.  
    Does anyone have experience with their objects falling through the floor and some suggested tactics for dealing with this?
     
  2. TimmyTheTerrible

    TimmyTheTerrible

    Joined:
    Feb 18, 2017
    Posts:
    186
    Use a collider with the rigidbody. Its the only way :)
     
  3. alexchesser

    alexchesser

    Joined:
    Sep 15, 2017
    Posts:
    147
    Thanks!

    Sounds like *not* falling through the terrain was the "bug" because there was no falling built into the root motion of my guy