- Home /
if statements for animations js
Hey there,
I'm stuck trying to order my animations correctly. I;ve tried lots of ways to cut it, but can't get to work I'm trying to create various states for various actions such as :
When running, colliding with an object, when jumping, when in air colliding with an object and also a death animation.
Here's the code so far:
///For Character Animation
var isHurt = false;
var isDead = false;
function Update(){
var moveDir = transform.forward * speed; // calculate the horizontal speed
if (!cc) cc = GetComponent(CharacterController); // get the CharacterController
if(cc.isGrounded) {
// when grounded...
vSpeed = 0.0;
//For stumble
if (isHurt == true) {
///THIS DOESN'T
if (isDead == true) {
Debug.Log("Animate Death");
Peter_Run15.animation.CrossFade("Idle",0.15f);
}
///THIS WORKS
Debug.Log("Animate Stumble");
Peter_Run15.animation.CrossFade("Idle",0.15f);
TurnoffHurt();
}
/// THIS WORKS
//For Jump
if (Input.GetButton ("Jump")) {
vSpeed = jumpSpeed;
Peter_Run15.animation.CrossFade("Jump",0.15f);
}
///THIS WORKS
//For Run
else {if (isHurt == false) {
//Animation code for the run
Peter_Run15.animation.CrossFade("Run",0.15f);
}
}
}
if(!cc.isGrounded) {
///THIS DOESN'T WORK
if(isHurt == true) {
Debug.Log("Animate Stumble");
Peter_Run15.animation.CrossFade("Stumble",0.15f);
TurnoffHurt();
}
/// NOT SURE IF THIS WORKS
else {if (isHurt == false) {
//Animation code for the run
Peter_Run15.animation.CrossFade("Jump",0.15f);
}}
}
vSpeed -= gravity*Time.deltaTime; // apply a physically correct gravity
moveDir.y = vSpeed; // add the vertical speed
cc.Move(moveDir*Time.deltaTime); // and finally move the character
}
///ALL THIS THE BELOW IS WORKING; TURNING ON AND OFF variables
///Function to turn off Hurt trigger for animation
function TurnOnHurt(){
isHurt = true;
Debug.Log("isHurt Turned ON");
}
function TurnoffHurt(){
yield WaitForSeconds(0.480); // time in Seconds how long your stumble animation is or for how long you want to wait until this animation can be played again.
isHurt = false;
Debug.Log("isHurt Turned OFF");
}
//Function for dead animation
function StartDeadAnimation(){
isDead = true;
Debug.Log("isDead Turned ON");
}
Any help would be much appreciated.
Cheers,
Rich
if (isHurt == true) {
///THIS DOESN'T
if (isDead == true) {
Debug.Log("Animate Death");
20. Peter_Run15.animation.CrossFade("Idle",0.15f);
}
///THIS WOR$$anonymous$$S
Debug.Log("Animate Stumble");
Peter_Run15.animation.CrossFade("Idle",0.15f);
25. TurnoffHurt();
}
If you change this to :
if (isHurt == true) {
if(isDead == false) {
Debug.Log("Animate Stumble");
Peter_Run15.animation.CrossFade("Idle",0.15f);
TurnoffHurt();
}
else {
Debug.Log("Animate Death");
Peter_Run15.animation.CrossFade("Idle",0.15f);
}
}
what happens?
Can't really see a problem here except that at the moment your Death Animation always gets overwritten by the stumble Animation.
Debug.Log should still print "Animate Death"...
Are both of the death/hurt states playing "Idle"?
It looks like they can jump even when isDead is true
If you use consistent indenting you will have an easier time scanning and modifying this code (use your IDE to autoformat if you prefer)
you have redundant checks on your elses, the else block will execute only when the if condition if false:
if(isHurt == true) { Debug.Log("Animate Stumble"); Peter_Run15.animation.CrossFade("Stumble",0.15f); TurnoffHurt();
} /// NOT SURE IF THIS WOR$$anonymous$$S
else {if (isHurt == false) { //Animation code for the run Peter_Run15.animation.CrossFade("Jump",0.15f); }}
is equivalent to:
if(isHurt) {
Debug.Log("Animate Stumble");
Peter_Run15.animation.CrossFade("Stumble",0.15f);
TurnoffHurt();
}
/// NOT SURE IF THIS WOR$$anonymous$$S
else {
//Animation code for the run
Peter_Run15.animation.CrossFade("Jump",0.15f);
}
TurnOffHurt will wait for 0.48 secs before setting isHurt to false, if your game is running at 60 fps this might result in 0.48 * 60 = 28 copies of TurnOffHurt stacked up before isHurt changes value.
Your answer
Follow this Question
Related Questions
How can I call an animation via script? 1 Answer
Animator Override Controller changed at runtime doesn't always play the animations correctly 1 Answer
Timeline and AnimationClip problem -1 Answers
Adding a playing animation onto a transform (with a transformation) 1 Answer
Remove unused animation curves? (Animation.Update() optimizing) 1 Answer