Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to get root rotation on last frame of animation clip (turn in place)

Discussion in 'Animation' started by Jshh, Nov 2, 2023.

  1. Jshh


    Sep 14, 2019
    Hi there,

    Newish to mecanim/humanoid animation so please forgive me if I'm overthinking this:

    I'm trying to create a turn-in-place system for humanoid navmesh npcs. I have animations for turning right/left, to 90 or 180 degrees (but they could be anything). I want to be able to specify a final rotation with more granularity than the animations available (eg. turn left 120).

    My plan is to add/remove a small amount of rotation per frame during the rotation animation, so that when the animation is finished, the character is pointing in the specified direction, and visible foot-sliding is minimized (though not completely removed, of course).

    For example, I want my agent to turn left 120 degrees, but the closest animation is left90, so we are 30 degrees out. We find the left90 animation clip length is 3 seconds, so we play it and add 10 degrees per second to the existing root motion rotation in `OnAnimatorMove()` . My hunch is this won't be too noticeable, and it'll be reasonably flexible.

    So I think I need to get the forward rotation of the npc on the last frame of the animation before it happens, and the total duration of the animation. Does anyone know if/how it's possible to get this data?

    I'm also transitioning from an idle into the turn animation over eg. 0.2s, so I think this might have an effect on the total amount turned, is that right? Any smart ways I can adjust for this?