I am new to Unity and I am working on my first 2D platformer… my player is using the CharacterController2D from here:
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);
}