- Home /
Problem with Reverse Animation
First off I know there are a bunch of "reverse animation problem questions..." but none seem to help with the problem I'm currently having.
I have an object with an animation, the animation is triggered when the player walks into a collider (set up as a trigger). Following script is attached to this collider:
var targetObject : GameObject;
var targetAnimation : String;
function OnTriggerEnter(other: Collider) {
targetObject.animation[targetAnimation].speed = 1;
targetObject.animation.Play(targetAnimation);
}
function OnTriggerExit(other: Collider) {
targetObject.animation[targetAnimation].speed = -1;
targetObject.animation.Play(targetAnimation);
}
the "targetObject" variable holds the said object which is to run it's animation and "targetAnimation" is the name of said animation.
the animation wrap mode is "clamp forever" by the way.
The problem I'm having now is following: 1. When the player walks into the collider the animation starts immediately (like I want it to) 2. Then When the Player leaves the collider the animation reverses with no apparent problem 3. But know the longer the player stands out of the collider and then steps back in...it takes longer for the animation to begin 4. The same when the player stays in the collider for a longer time, the reverse animation then also takes longer to start again.
I'm not really sure whats causing this and I want to thank you for any answers in advance. I hope to able to contribute to this great community with answers of my own once I'm a more skilled unity user ;-)
Answer by Owen-Reynolds · Jan 05, 2012 at 12:26 AM
Not tested, but...say your animation takes 2 seconds. The time runs 0,1,2,3,4... and anything past 2 counts as 2. When you reverse, it still counts down from 4 (counting as 2,) appearing to freeze until it hits 2. Likewise, it reverses to -1, -2... . Restarting has to count up to 0.
A fix might be adding:
if(targetObject.animation[targetAnimation].time<0)
targetObject.animation[targetAnimation].time=0; // onEnter
if(targetObject.animation[targetAnimation].normalizedTime>1)
targetObject.animation[targetAnimation].normalizedTime=1; // onExit
The idea of the ifs is if you happen to play 1/2 of the animation, you just want to play backwards from there -- not snap to the end then reverse.
Thank you Owen! Worked like a charm!
One more questions: is there a reason not to use "normalizedTime" in both cases ?
For 0, time and normalizedTime are the same (normalizedTime is just time scaled to 0-1) -- time is just shorter to write (and probably runs a hair faster, but not worth worrying about.)