Search Unity

Resolved Gravity after animation

Discussion in 'Animation' started by PenProd, Jan 15, 2023.

  1. PenProd

    PenProd

    Joined:
    Dec 17, 2022
    Posts:
    171
    II have a book that's in a book case. When a user clicks on it, I want to animate it sliding out of the book case, after which it should fall to the ground. Since I'm new to Unity, I'm a bit confused how to do this.

    The book has a box collider with "Is Trigger" enabled (so you can click on it). The book also has the RigidBody component and if I enable gravity, it simply falls through the bookcase and even trough the floor.

    So how can I get it to do what I want?
     
  2. PenProd

    PenProd

    Joined:
    Dec 17, 2022
    Posts:
    171
    Ok, I'm beginning to understand a little bit more in which direction I should go from here.

    First of all, I disabled the book's "Is Trigger" so it properly collides with the book case it's in. Then I created an empty child gameobject with a box collider that will serve as the trigger.

    On the book itself, I have enabled "Gravity" as well as "Is Kinematic" so I can move it using an animation without physics being applied to it. Then when the book is in its final position, I can disable "Is Kinematic" and gravity should take over. That sounds great in theory.

    But apparently, I cannot use an Animator component to move the object. Because when the animation ends, the books position stays fixed at that point and gravity can't change it. Because I haven't animated it's rotation, as soon as I disable "Is Kinematic", the book starts rotating wildly while it's position in space remains the same.

    I thought that enabling "Apply Root Motion" would fix this, but then the movement for some reason gets reversed. So instead of moving out of the book case, it moves in the opposite direction.

    So am I correct that I can't use the Animator component in this case? Can I only animate through a script?
     
  3. PenProd

    PenProd

    Joined:
    Dec 17, 2022
    Posts:
    171
    Another update.

    I found out I can use the Animator Component to move an object with "Apply Root Motion" enabled, as long as the object's orientation is the same as the world orientation. Let me try to explain.

    Before, I had placed the book inside the bookcase but had rotated it 180 degrees around its Y-axis. I then made an animation to move the book out of the bookcase. But since the book's orientation was now rotated 180 relative to the world orientation, I actually had to decrease it's Z value. Since from the book's perspective, it had to move backwards.

    When you apply root motion, it adds the animation's value to its world position. So if the animation decreases its Z-value (which it did) than it will actually now move the other way.

    So what I ended up doing, was set the book's x/y/z position and rotation to 0, create an empty parent object and moved/rotated the parent object to its desired position. Now when I animate the book, it will always move the same whether "Apply Root Motion" is enabled or not.

    So that took care of that. I set the book's "Is Kinematic" to true, animate the book and at the end of the animation set "Is Kinematic" to false again so physics/gravity takes over again.

    That worked - kinda...

    After I set "Is Kinematic" to true, the book behaves very, very strangely. If I move the book to a point where it's still on the bookshelf, but with its center of gravity just over the edge and I set "Is Kinematic" to true, it starts to tip over (as it should) but then suddenly "jumps" of the shelf, landing a good 3 feet away from the bookcase. If I animate it to also rotate a bit sideways (since I want the book to land on its side), so that when physics/gravity takes over, it should rotate further, it actually does that, falls of the shelf and then drops to the floor like a feather (slow and zig-zagging). It looks funny as hell, but not what I intended.

    Weight is set to 1, Drag is set to 0 and Angular Drag set to 0.05 so it should simply drop to the floor as a book...
     
  4. PenProd

    PenProd

    Joined:
    Dec 17, 2022
    Posts:
    171
    After quite a bit of trial and error, I found out that the only way to have the book behave "normally" after the animation, is to not only set "Is Kinematic" to false, but also completely disable the Animator.