- Home /
The question is answered, right answer was accepted
Animation doesn't play all the way through unless holding the key down
I am new to Unity and I am working on my first 2D platformer.. my player is using the CharacterController2D from here:
https://github.com/prime31/CharacterController2D
I have various animations, like Idle, Walk, Jump, Kick, Punch, etc.. and for the most part they're working just fine. However, the Kick animation consists of 2 sprites (StartKick and EndKick). When I press the key to kick, it only plays the animation half way (to StartKick)... I need to hold the key down to make it play all the way. This is unsatisfactory; I would like to just tap the key and make the kick work all the way without having to hold the key down.
What can I do?
EDIT
By the way, here's my Update method in a script attached to the player (which makes use of the CharacterController2D that I mentioned above:
public void Update()
{
// grab our current velocity to use as a base for all calculations
velocity = controller.velocity;
if (controller.isGrounded)
{
velocity.y = 0;
}
if (Input.GetKey(KeyCode.D))
{
//some logic
}
// lots of else ifs here... (removed to make it clearer), then for kick:
else if (Input.GetKey(KeyCode.E))
{
normalizedHorizontalSpeed = 0;
if (controller.isGrounded)
{
animator.Play(Constants.AnimationStateHashes.Rick.Kick);
}
}
else
{
normalizedHorizontalSpeed = 0;
if (controller.isGrounded)
{
animator.Play(Constants.AnimationStateHashes.Rick.Idle);
}
}
// we can only jump whilst grounded
if (controller.isGrounded && Input.GetKeyDown(KeyCode.W))
{
velocity.y = Mathf.Sqrt(2f * jumpHeight * -gravity);
animator.Play(Constants.AnimationStateHashes.Rick.JumpOrBlock);
}
// apply horizontal speed smoothing it
var smoothedMovementFactor = controller.isGrounded ? groundDamping : inAirDamping; // how fast do we change direction?
velocity.x = Mathf.Lerp(velocity.x, normalizedHorizontalSpeed * runSpeed, Time.deltaTime * smoothedMovementFactor);
// apply gravity before moving
velocity.y += gravity * Time.deltaTime;
controller.move(velocity * Time.deltaTime);
}
@meat5000, I don't see anything called Exit Time and after Googling a bit, I think you're referring to something on the animation transitions. I don't have any transitions right now, since I am manually playing the clips myself (You can see this in my code above) and not using the mecanim state machine.
I guess the 'problem' is the
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.E))
{
...
}
else
{
...
}
logic.
If you stop pressing the 'E'-$$anonymous$$ey, your program enters 'else' and starts playing the 'idle'-animation. You should check if your 'kick'-animation has finished before starting the 'idle'-animation.
Answer by meat5000 · Feb 02, 2015 at 02:42 PM
It appears that it is becoming interrupted due to the if else logic.
When you release the E key, on the next Update the else is triggered which makes the idle animation kick in. Its really playing less than half but you see half due to the transition to idle, I do believe.
If you don't want to use the State Machine you should implement states in your code instead. You can do this with a switch perhaps. Realistically though, you need to check which animation is currently playing before you call another. This could also be tricky without the state machine as nothing is layered.
Thanks. In the end I actually decided to work with the Unity State $$anonymous$$achine; it's great.
Follow this Question
Related Questions
SpriteManager 2 1 Answer
Sprite animation 2 Answers
How to make not smooth animation? 0 Answers
Animation System in 4.3 1 Answer