- Home /
How to increase and decrease the animation speed?
What I need is what the speed of the player is, the animation should go faster and slower. Right now, I this little snip of code which Unity doesn't seem to like it. It also seems that I am not able to access the animation in the 'Animation Editor'. I also added an 'Animation Component' to the Player and from my animations folder, I dragged the animation onto it. If you could help me, it would be greatly appreciated. Thank you.
//Animation
var absVel = Mathf.Abs (rigidbody2D.velocity);
if (absVel < 0.1f) {
animation["WhaleSwimAnimation"].speed = 0.1f;
} else if (absVel < 0.5f && absVel > 0.1f) {
animation["WhaleSwimAnimation"].speed = 0.3f;
} else if (absVel < 1 && absVel > 0.51f) {
animation["WhaleSwimAnimation"].speed = 0.7f; }
else if (absVel < 2f && absVel > 1f) {
animation["WhaleSwimAnimation"].speed = 1f;
}
Do you get any errors to this code? You can also try to use: rigidbody2D.velocity.$$anonymous$$agnitude
With the $$anonymous$$agnitude I do not get any errors except one which just says GetRef. I have that error 4 times which appears at every "animation["WhaleSwimAnimation"].speed = 0.1f;". I am still not bale to change the speed though.
Yes, that's what I did which gets me the 'NullReferenceException: GetRef' error.
Answer by Xysch · Nov 08, 2014 at 04:59 AM
since it's 2D, I found out that you would have to use this:
var absVel = Mathf.Abs (rigidbody2D.velocity.magnitude);
if (absVel < 0.1f) {
anim.speed = 0.1f;
} else if (absVel < 0.5f && absVel > 0.1f) {
anim.speed = 0.3f;
} else if (absVel < 1 && absVel > 0.51f) {
anim.speed = 0.7f;
} else if (absVel < 2f && absVel > 1f) {
anim.speed = 1f;
}