Hey All,
Once again I’m stumped. I think it’s because I used the video to make my movement system. I might switch to “Horizontal” and “Vertical” but right now I don’t have the energy:
Which, the movement works great, but I want to implement cancelling the movement input when an attack animation is being played. I’ve tried everything, but can’t seem to come up with a solution. Code below. Please be nice, I know it’s messy and I’m super new to this. All suggestions welcome.
public class ControllerCharacter : MonoBehaviour
{
public float speed = 20f;
private Rigidbody2D myRB;
private Vector3 moveDir;
private Vector3 dashDir;
public float dashSpeed;
public float dashCooldownTime = 0.5f;
private enum State
{
Normal,
Dashing,
}
private Animator myAnim;
private bool isDodgeButtonDown;
public ParticleSystem dust;
public float dodgeCooldownTime = 0.5f;
private float nextDodgeTime = 0;
private float nextDashTime = 0;
private State state;
private void Awake()
{
myRB = GetComponent<Rigidbody2D>();
myAnim = GetComponent<Animator>();
state = State.Normal;
}
private void Update()
{
switch (state)
{
case State.Normal:
float moveX = 0f;
float moveY = 0f;
if (Input.GetKey(KeyCode.W))
{
moveY = +1f;
}
if (Input.GetKey(KeyCode.S))
{
moveY = -1f;
}
if (Input.GetKey(KeyCode.A))
{
moveX = -1f;
}
if (Input.GetKey(KeyCode.D))
{
moveX = +1f;
}
moveDir = new Vector3(moveX, moveY).normalized;
myAnim.SetFloat("MoveX", moveX);
myAnim.SetFloat("MoveY", moveY);
if (moveX == 1 || moveX == -1 || moveY == 1 || moveY == -1)
{
myAnim.SetFloat("LastMoveX", moveX);
myAnim.SetFloat("LastMoveY", moveY);
}
if (Time.time > nextDodgeTime)
{
if (Input.GetKeyDown(KeyCode.C))
{
//isDodgeButtonDown = true;
myAnim.SetBool("IsAttacking", true);
nextDodgeTime = Time.time + dodgeCooldownTime;
}
}
if (Time.time > nextDashTime)
{
if (Input.GetKeyDown(KeyCode.Space))
{
myAnim.SetBool("IsDashing", true);
dashDir = moveDir;
dashSpeed = 80f;
state = State.Dashing;
CreateDust();
nextDashTime = Time.time + dashCooldownTime;
}
}
if (Time.time > nextDashTime)
{
if (Input.GetKeyDown(KeyCode.V))
{
myAnim.SetBool("IsSlashing1", true);
//dashDir = moveDir;
//dashSpeed = 2f;
//state = State.Dashing;
nextDashTime = Time.time + dashCooldownTime;
}
}
if (Time.time > nextDashTime)
{
if (Input.GetKeyDown(KeyCode.B))
{
myAnim.SetBool("IsSlashing2", true);
//dashDir = moveDir;
//dashSpeed = 2f;
//state = State.Dashing;
nextDashTime = Time.time + dashCooldownTime;
}
}
break;
case State.Dashing:
float dashSpeedDropMultiplier = 5f;
dashSpeed -= dashSpeed * dashSpeedDropMultiplier * Time.deltaTime;
float dashSpeedMinimum = 50f;
if (dashSpeed < dashSpeedMinimum)
{
state = State.Normal;
}
break;
}
}
private void FixedUpdate()
{
switch (state)
{
case State.Normal:
myRB.velocity = moveDir * speed;
if (isDodgeButtonDown)
{
float dodgeAmount = 2f;
Vector3 dodgePosition = transform.position + moveDir * dodgeAmount;
myRB.MovePosition(transform.position + moveDir * dodgeAmount);
isDodgeButtonDown = false;
}
break;
case State.Dashing:
myRB.velocity = dashDir * dashSpeed;
break;
}
}
}