When you use a walk animation in a Unity Timeline using an Animation Track with Scene Offsets applied, you may come across a problem where you want to have the character end up at a precise location at a specific time. The problem is if you are in Preview mode (the default), moving the character in the scene is not preserved. As soon as you scrub the timeline playhead, your change is lost. This post I share a hacky little script that lets you do accurate positioning.
To expand upon the introduction, consider an animation track for a character that has a walk animation clip – something that moves the character from its original position. If you want it to end standing in a particular position at the end of the clip at a specific time, it can be a bit fiddly. The problem is while the Timeline window is in “preview” mode (to see the effect of the animation clip), if you move the character your movement is overridden by the preview mode.
Note: This is only relevant when you have “Apply Scene Offsets” in effect. This is where the starting position of the character is used as an offset to the animation clip, allowing the character to walk from its starting position in the scene.
To move the character, what you need to do is either turn preview mode off or mute the animation track. The character will then jump back to the starting point in the scene clip and you can move it. After moving, you can then turn preview mode back on.
This works. It’s just inconvenient as you have to judge how far to move the character. For example, in preview mode I sometimes write down the original position of the character, move it to where I want it to be, write that down, then turn off preview mode. I then work out the difference between the two positions and apply that delta to the start position of the character. (Okay, what I really do is exit preview mode, move it, go back into preview mode, see if it looks good, then repeat until I get it right. I mean who wants to write things down and do maths?)
So I wrote some script to automate the above. (Gist link to source code.) I tried to move the character via script while in preview mode, but to date have failed. So the script works by making you use a menu item to mark the first position (while in preview mode), then you move the character and mark the second position (while still in preview mode), then you turn off preview mode and do a third operation to move the character based on the previous two positions marked. That is, it does what I described above with pen and paper using a script.
For example, here are two characters. I want Ivy on the left to walk up to and stop in front of Mandy on the right at exactly the 2 second mark.
Scrubbing the playhead forward to 2 seconds, you can see the starting position for Ivy is currently not correct.
So right click on Ivy’s animation track and pick “Mark Object Start Position”.
Then move Ivy in the scene to the desired position.
Back to the animation track menu to mark the new position using “Mark Object End Position”. (As soon as you move the playhead, this position will be lost.)
Click the “Preview” button (top left corner of screenshot above) to exit preview mode. Ivy will jump back to her initial position in a T-pose.
Right click on the animation track again and select “Move to Position”.
Ivy will then move by the required amount in the scene.
Turning preview mode back on, Ivy at 2 seconds is now at the correct position.
How to improve?
If I could turn off preview mode from inside the script, this could be simplified to a two step operation. Still exploring options, but everything I have tried to date has not worked. (If you know the answer, please let me know and I will update this blog post!)
But my “ideal” solution would be for Unity to change the behavior of the “move” operation. If the Timeline window is open and there is an animation track controlling the character (with “Scene Offsets” on), then make the move operation change the original (scene) position of the character. I don’t see much use in the current functionality – you move the character and it gets lost as soon as you move the playhead. So while a change in behavior, it seems a useful change… to me anyway!