Root Motion prevents transform in animation from running
I have two layers in my Unity 2D animation; the base layer is an endless loop animating the object's sprite, and the other layer contains an animation which modifies the object's transform when the player lands on it.
This trivial animation - involving a single transform - isn't working when I enable root motion on the animator.
This animation is run by the following on the object:
void OnCollisionEnter2D(Collision2D other)
{
animator.Play("Bounce", bounceLayerIndex, 0f);
}
The player has an associated OnCollisionEnter2D() which makes it a child of the object.
This animation is trivial, containing a keyframe with transform (0,0) and another with transform (0,-0.3f). The idea is that when the player lands on the object, the object responds slightly.
My difficulty here is with root motion. When I don't have root motion applied, everything works fine - except I need to have the object inside an arbitrary parent since I'm applying a transform in the animation. The parent is positioned where I want the object to be, and the object is positioned at (0,0) relative to it.
I found this approach a bit klunky, particularly since there are going to many of these objects, all at different positions in 2D space. I therefore looked into root motion, which appears to be suitable for this use case. I checked the 'Apply root motion' checkbox on the animator on the object and clicked the 'Generate Root Motion' curves on my animation.
The animator indicates that the animation is running as expected when the player collides, however the transform is having no effect.
Interestingly, when I moved the animation to the base layer (and disabled the endless sprite animation), the transform animation does run correctly - but I then get an odd endless loop of OnCollisionExit2D()/OnCollisionEnter2D() as the object moves. The player is a child of the object at this point, so this behaviour is unexpected.
I'm clearly misunderstanding how root motion is supposed to work. I assumed that if my object was at (0, 1f) then with root motion applied, my animation transform of (0, -0.3f) would animate the object to (0, 0.7f) - namely, relatively to the object's position.
I'm using Unity 5.6.0f3 personal.
FWIW, I added a non-Transform property to the animation (a sprite renderer X flip, just for something quick) and this did get animated correctly. Just no position, or rotation for that matter.