- Home /
How to get animated sprite to adhere to gravity?
I am making a gravity-basics puzzle game where the player rotates the world to navigate the hero (ball) through each level. Things were going pretty well until I tried to animate one of the baddies. Once I added an animation (created using the built in animation tab), he seemed to simply ignore gravity (though he still reaction to things hitting his collider).
I took a quick video of this behavior. You can see that he falls to the ground naturally at the beginning, but when the world rotates, he kinda does his own thing (ignore the paranoid sun, that's supposed to do that).
There are no scripts on the block guy, just a the following modules:
Does the animation by any chance animate the global position of the object?
It does, now that you mention it. It's using global transforms. Can I switch that to local, somehow, within the animation panel?
There doesn't seem to be a way to do it within the animation panel. I've spent quite a few hours painstakingly moving keyframes from one curve to another... Grr.
What you CAN do, though, is make a parent for your animated object and maybe copy the collider and rigidbody into that (or not, if you need the collider animated). Then, your animation will work on local space without having to redo the object tree in the animation pane ("animation pain", lol). Note that this may break your global transforms, so make sure they're relative to 0 ins$$anonymous$$d of relative to the global coordinates after you've made the move.
You can't have "local" transformations of the "global" coordinates because you wouldn't have any sort of reference frame, so either the animations or your transformations would screw up. What @Pyrian suggested is a pretty good workaround.
@Pyrian - Yes! Putting the collider and rigidbody on an empty object and then putting the sprite renderer and animator on the child object works great.
Note! If you compose the animation while the controller is already parented, it seems to record the tranforms with the local origin. So I just remade the anims and now I'm in business!
Answer by justaleaf · Apr 16, 2014 at 01:16 AM
If someone has a better option, I'm all ears, but as @Pyrian noted in the comments, the following solution seems to work well.
Import your sprite as per usual, setting up your collider and Rigidbody, scripts that effect physics, etc. The collider wraps to the sprites dimensions which will save just a little time.
Then, remove the sprite renderer component from the object, and add the sprite as a child.
So, in my project the parent looks like:
And the child looks like:
Now that the child is inheriting it's parents transforms, you should be able to add/create animations freely, working from the transforms relative to the child.
Don't know whether it will screw up the rest of your animation, but setting the animation mode to additive might also work. I'm not able to test it right now though.
Answer by ReContraChanfle · Apr 15, 2014 at 05:49 AM
have you tried with the "Animate Physics" in your Animator Inspector.
Yeah, it's pretty funny to watch. But it's not compatible to gravity.
AnimatePhysics
allows animations to have a physical effect, not the other way around.