Hello,
As said in the title I currently am trying to replicate the way that the Zelda gameboy games handle sword swinging. The thing I am specifically struggling with is that you are able to spam the attack button to swing rapidly. This is shown in the beginning of this video:
This is currently what my PlayerController currently looks like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum PlayerState {
walk,
attack,
interact
}
public class PlayerMovement : MonoBehaviour
{
public PlayerState currentState;
public float speed;
private Rigidbody2D playerRigidbody;
private Vector3 change;
private Animator anim;
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
playerRigidbody = GetComponent<Rigidbody2D>();
currentState = PlayerState.walk;
}
// Update is called once per frame
void Update()
{
change = Vector3.zero;
change.x = Input.GetAxisRaw("Horizontal");
change.y = Input.GetAxisRaw("Vertical");
if (Input.GetButtonDown("attack") && currentState != PlayerState.attack)
{
StartCoroutine(AttackCo());
}
else if (currentState == PlayerState.walk)
{
UpdateAnimationsAndMove();
}
}
private IEnumerator AttackCo()
{
anim.SetBool("Attacking", true);
currentState = PlayerState.attack;
yield return null;
anim.SetBool("Attacking", false);
yield return new WaitForSeconds(0.25f);
currentState = PlayerState.walk;
}
void UpdateAnimationsAndMove() {
if (change != Vector3.zero)
{
MovePlayer();
anim.SetFloat("movex", change.x);
anim.SetFloat("movey", change.y);
anim.SetBool("moving", true);
}
else
{
anim.SetBool("moving", false);
}
}
void MovePlayer() {
playerRigidbody.MovePosition(transform.position + change * speed * Time.deltaTime);
}
}
This code recreates the base movement and sword swinging pretty closely, however you can’t attack quickly like shown in the video. Any suggestions on how I can implement this?
Thanks!