- Home /
Mecanim: Backward animations only work in preview, not in game
Hello,
I have a blend tree that works fine when the enemy is set to be moving forward, but when going backward, the legs do not move. The preview for the Animator component shows the character (model and all) moving backward when I slide the "speed" toggle to less than zero, but when the game is running, and the speed is set to less than zero, the legs simply stand still.
The character is being controlled by a navmesh, with the speed and angular speed being passed to mecanim to display the proper animations.
Any idea why this may be breaking during game?
Answer by infinitypbr · Jun 26, 2013 at 06:04 AM
Ok, let it never be said that I'm not an idiot a lot of the times.
"Loop Pose" wasn't checked.
Answer by DecipherOne · Jun 25, 2013 at 11:03 PM
I would check and see if there is a bounds restriction in a part of your code that will not let the animation play backwards at runtime.
I'm not sure what you mean by bounds restrictions, but I haven't written any restrictions or anything.
Answer by TonyLi · Jun 26, 2013 at 12:30 AM
I think @DecipherOne is talking about something in your code that's preventing the speed parameter from being set to a negative number.
For example, NavMeshAgents don't walk backwards; they turn instead. The speed is never going to be less than zero.
When the game is running, select the enemy and look at the Animator window. Make sure the value of the speed parameter is what you expect.
Yep, @TonyLi that's exactly what I was saying. Check and make sure there isn't a bit of code somewhere that keeps your speed from being negative when you're actually running the game.
Ah, yes, I see the negative value in the animator during runtime, exactly as expected. Here's the code I use, with a little segment that checks to see if the direction the object is traveling in is the same direction it's facing or not. If they're facing the wrong way, I make the movement speed negative.
var positionDistance = Vector3.Distance(enemyLastPosition, transform.position);
var movementSpeed = (positionDistance * (1 / Time.deltaTime));
var facingDifference = Vector3.Distance(enemyLastPosition, Vector3(transform.position.x, transform.position.y, transform.position.z + 1));
// FIND OUT IF ENE$$anonymous$$Y IS WAL$$anonymous$$ING BAC$$anonymous$$WARD -- Facing opposite direction they're walking
if (facingDifference > 1)
{
movementSpeed = movementSpeed * -1;
}
print ("movementSpeed: " + movementSpeed);
enemyAnimator.SetFloat ("Speed", movementSpeed);
I found this after a quick search. I'm not really familiar with mecanim myself, but this http://forum.unity3d.com/threads/167951-$$anonymous$$ecanim-Use-Case-play-animation-from-any-frame-forward-or-reverse thread suggest that sort of functionality isn't available through mecanim currently.
That's a good thought from @DecipherOne. But since you can preview it in the editor, you must be using a separate animation for retreating (or at least another copy of the animation with Speed=-1). So that should work fine.
Sounds like an issue with the animation state machine. That's where I'd check -- make sure it's transitioning properly into the blend tree, and that the blend tree is using the parameter values in the way you're expecting.
Yeah, right now all the animations are in one 2d blend, and I think I'll try to make two blends, one for forward and one for back, with an "isBackward" variable or something.