Animation Shifting


I’ve got an enemy character with dozens of animations. Many of these animations (especially since they’re mo-capped), shift the enemy mesh off of the origin. I have a base game object with my AI scripts and character controller attached, under which the rig and skinned mesh reside.

The problem: When the enemy plays an animation that shifts him off of his local origin, such as my transitional animation from idle to an alert state (he sort of stumbles backwards startled), I need a clean way to recalibrate the base object so the next time I play an animation, it’s relative to the new direction.

Here’s a better example: The enemy is aiming at the player. The player strafes around the enemy, forcing the enemy to play a “90 degree turn animation”. If the player continues to strafe, the enemy will play that animation again (to face the player who is now behind what the enemy’s original orientation was) however the animation will play from his original forward, ending up facing the same direction (90 degrees right or left).

My attempted code fix:

// Recalibrate Base
// .,;,.,;,.,;,.,;,.,;,.,;,.,;,.,;,.,;,.,;,.,;,.,;,.,;,.,;,.,;,.,;,.,;,.,;,.,;,.,;,,;,.,;,.,;,.,;,
void RecalibrateBase()
	Vector3 RootPos = m_RigRoot.transform.position;
	Quaternion RootRot = m_RigRoot.transform.rotation;
	Vector3 RigForward = m_RigRoot.transform.forward;
	RigForward.y = 0.0f;
	transform.position = RootPos - ( Vector3.up * m_RigRootOffset );
	transform.rotation = Quaternion.LookRotation( RigForward );
	m_RigRoot.transform.position = RootPos;
	m_RigRoot.transform.rotation = RootRot;

Unfortunately, when I call that function after I determine the transitional animation has completed (all I do is check m_Animation.IsPlaying( m_TransitionalAnimation ) to see if it’s done), the enemy pops a bit…each subsequent call sends the enemy further and further into the ground like quicksand. Funny, sure…but not helpful. :slight_smile:

Sorry for the unnecessarily long post…the main question is, has anyone encountered this kind of problem or, more importantly, know of a better way to handle recalibrating a base object that has an animated mesh below it whose direction/position is crucial?

Thanks for the help!

I think I figured out what’s wrong. The child bone (the rig root) that I’m trying to manually offset based on the parent’s new position is likely getting moved by the animation.

In other words I say “parent go here”, “now child move in the opposite direction so that you’re still in the same world position”. Then the animation comes along and overrides the bone’s position based on the animation playing.

That’s my assumption, I’ll test it now and see. :slight_smile: