- Home /
Scripting-Animation Issue
In the prototype I'm working on, I have it so the space bar makes the character jump and the Fire1 button makes the character perform a random attack (out of the two I have animated). The movement controls are the typical W,A,S,D setup and the Fire2 button can orbit the camera around your character.
I've gotten the crossfades to work nicely while holding down the appropriate keys to move the character forward, backward, pivot left, and pivot right (I haven't restricted them to stop the pivot animations from playing while running and turning just yet, but that will be for later. I also have two attack animations, and a temporary jump animation. I'm still messing with the jump animation for it's timing at the appropriate parts of the jump, so for the time being, it's just supposed to play the whole thing at the standard frame-rate.
The issue is that while the W,A,S,D buttons require to be held down to loop through their animations and crossfade back to idle while they're not being held down, the space bar and Fire1 are supposed to just continue playing after the click or touch, but they don't. Regardless if I use GetButtonDown or GetButton, I get the same result. Any suggestions?
for (var i: KeyCode in zikkuKeys)
{
if(Input.GetKey(i))
{
switch(i)
{
case KeyCode.W:
PerformSomething(ZikkuState.Running);
break;
case KeyCode.A:
PerformSomething(ZikkuState.PivotLeft);
break;
case KeyCode.S:
PerformSomething(ZikkuState.MoveBackwards);
break;
case KeyCode.D:
PerformSomething(ZikkuState.PivotRight);
break;
case KeyCode.Space:
if(canJump == true)
{
PerformSomething(ZikkuState.Jumping);
}
break;
}
}
if(Input.GetButtonDown("Fire1"))
{
PerformSomething(ZikkuState.Attacking);
}
else if(!Input.anyKey)
{
PerformSomething(ZikkuState.Idle);
}
And here's the function Perform Something that explains what each acceptable input does.
function PerformSomething(currentState : ZikkuState)
{
movePlayer = (Vector3.forward*Time.deltaTime);
turnPlayer = (Vector3.up*Time.deltaTime);
switch(currentState)
{
case ZikkuState.Idle:
zikkuChar.animation.CrossFade("idle", 0.2);
break;
case ZikkuState.Running:
transform.Translate(movePlayer * runSpeed);
zikkuChar.animation.CrossFade("run", 0.2);
zikkuChar.animation.wrapMode = WrapMode.Loop;
break;
case ZikkuState.MoveBackwards:
transform.Translate(-movePlayer * moveBackSpeed);
zikkuChar.animation.CrossFade("moveBackward", 0.2);
zikkuChar.animation.wrapMode = WrapMode.Loop;
break;
case ZikkuState.PivotLeft:
transform.Rotate(-turnPlayer * turnSpeed);
zikkuChar.animation.CrossFade("pivotLeft", 0.2);
zikkuChar.animation.wrapMode = WrapMode.Loop;
break;
case ZikkuState.PivotRight:
transform.Rotate(turnPlayer * turnSpeed);
zikkuChar.animation.CrossFade("pivotRight", 0.2);
zikkuChar.animation.wrapMode = WrapMode.Loop;
break;
case ZikkuState.Jumping:
lastJumpButtonTime = Time.time;
zikkuChar.animation.CrossFade("JumpTemp", 0.2);
zikkuChar.animation.wrapMode = WrapMode.Once;
print("Zikku jumped.");
break;
case ZikkuState.Attacking:
ZikkuAttacks();
break;
default:
zikkuChar.animation.CrossFade("idle", 0.2);
break;
}
}
It was later discovered that my last else if statement was what was doing it. I had to change it to look to see if any other state was false before setting idle to true.
Your answer
![](https://koobas.hobune.stream/wayback/20220613064240im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Light switch trigger 3 Answers
Switching cameras (JavaScript) 0 Answers
Enum and Variable with similar name resulting in error 1 Answer
js switch case warning 1 Answer
Switch Statement question 0 Answers