I’m prototyping in Unity2D as a beginner, and I got the top-down movement working, as well as a basic attack. However, I’m trying to implement a Dash&Attack when the player presses the run button and the attack button, so the player character speeds up, moves a few units, and returns to regular speed.
I’d like to lock the player so the character executes the dash in the facing direction when initiated, without being able to change direction based on input (or have a shorter dash); however, input is still being processed, even though I’ve tried many conditions to prevent that.
My Movement Controller is on a separate script from the Combat Controller (which deals with basic attack and Dash & Slash logic). Here are some code excerpts:
private void Update()
{
GatherInput();
}
private void GatherInput()
{
_moveDirection.x = Input.GetAxisRaw("Horizontal");
_moveDirection.y = Input.GetAxisRaw("Vertical");
_isRunning = Input.GetButton("Run");
}
private void FixedUpdate()
{
UpdateAnimations();
}
private void UpdateAnimations()
{
if (_moveDirection != Vector2.zero)
{
_pc_Animator.SetBool("isMoving", true);
MovementUpdate();
}
else{
_pc_Animator.SetBool("isMoving", false);
_pc_Animator.SetBool("canMove", false);
}
if(Time.time > timeToBlink)
{
doBlinkRoutine = DoBlink();
StartCoroutine(doBlinkRoutine);
}
}
private void MovementUpdate()
{
_moveDirection = new Vector2(_moveDirection.x,_moveDirection.y).normalized;
facingDirection = _moveDirection;
if (canMove)
{
if (!_isRunning)
{
_pc_RB.MovePosition(_pc_RB.position + _moveDirection * _moveSpeed * Time.fixedDeltaTime);
_pc_DirtAnimator.SetBool("isRunning", false);
}
else
{
_pc_RB.MovePosition(_pc_RB.position + _moveDirection * _moveSpeed * _runningSpeedMultiplier * Time.fixedDeltaTime);
_pc_DirtAnimator.SetBool("isRunning", true);
}
}
_pc_Animator.SetBool("canMove", canMove);
_pc_Animator.SetFloat("moveX", _moveDirection.x);
_pc_Animator.SetFloat("moveY", _moveDirection.y);
}
else
{
//_pc_RB.MovePosition(_pc_RB.position);
}
}
And here’s the logic for Dash and Slash:
private void Update()
{
CheckCombatInput();
CheckAttack();
}
private void FixedUpdate()
{
RunSlashMovement();
}
private void CheckCombatInput()
{
if (combatEnabled)
{
if (Input.GetButtonDown("Attack"))
{
//Atack
gotAttackInput = true;
lastInputTime = Time.time;
}
if (Input.GetButton("Run") && Input.GetButtonDown("Attack") && _pcController.GetMoveDirection() != Vector2.zero)
{
//Run & Slash
gotAttackInput = true;
lastInputTime = Time.time;
isRun_Slash = true;
}
}
}
private void CheckAttack()
{
if(gotAttackInput)
{
if(!isAttacking && !isRun_Slash)
{
gotAttackInput = false;
isAttacking = true;
//Do Anim stuff
_pc_Animator.SetBool("isAttacking", isAttacking);
//Sword Trail
_pcSwordTrailTransform.position = attackHitBoxPos.position + (facingDirectionIn3D * 0.5f);
_pcSwordAnim.SetBool("isAttacking", isAttacking);
_pcSwordAnim.SetFloat("posX", facingDirectionIn3D.x);
_pcSwordAnim.SetFloat("posY", facingDirectionIn3D.y);
//Play Sound
PlaySwordAttackSFX();
} else if (isRun_Slash) {
if (Time.time >= runSlashCooldown)
{
gotAttackInput = false;
//Do Anim stuff
//_moveDirection = _pcController.GetMoveDirection();
_pc_Animator.SetBool("isRun&Slash", isRun_Slash);
_pcSwordAnim.SetBool("isRun&Slash", isRun_Slash);
_pcSwordAnim.SetFloat("posX", facingDirectionIn3D.x);
_pcSwordAnim.SetFloat("posY", facingDirectionIn3D.y);
//PLAY SOUND FX
PlaySwordAttackSFX();
runSlashCooldown = Time.time + 1f;
}
}
}
if (Time.time >= lastInputTime + inputTimer)
{
gotAttackInput = false;
}
}
private void RunSlashMovement()
{
if (isRun_Slash) {
_pcRB.MovePosition(_pcRB.position + new Vector2(_pcController.GetMoveDirection().x, _pcController.GetMoveDirection().y) * 3.25f * _pcController._moveSpeed * Time.fixedDeltaTime);
}
}