Question by
nigelsherrier · Mar 19, 2020 at 04:35 PM ·
movementdashreview
Cant Fix dash code in my movement script
Hello! Novice coder here. So im trying to write in a dash for my movement , it all works , while moving and while in the air.
But it doesnt work when the player is idle.
I want the movement to still come to a complete stop if there's no input, but allow for a dash from idle. id really appreciate any help! Ive been slowly working through this script for about 3 days now.
private void Movement()
{
float hDirection = Input.GetAxisRaw("Horizontal");
//kills movement if there's no horizontal(x.axis) input
if (hDirection == 0)
{
rb.velocity = new Vector2(0, rb.velocity.y);
}
//moving left
if (hDirection < 0 )
{
if (rb.velocity.x < -speed) //checks if were going beyond our base speed, if so, keep it. thus allowing dash while moving
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y);
}
else
rb.velocity = new Vector2(-speed, rb.velocity.y);
transform.localScale = new Vector2(-1, 1);
}
//moving right
else if (hDirection > 0 )
{
if (rb.velocity.x > speed) //checks if were going beyond our base speed, if so, keep it. thus allowing dash while moving
{
rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y);
}
else
rb.velocity = new Vector2(speed, rb.velocity.y);
transform.localScale = new Vector2(1, 1);
}
//Dash left and right
if (Input.GetKeyDown(KeyCode.RightShift) && transform.localScale.x == -1)
{
//dash left
rb.velocity = new Vector2(-dashing, rb.velocity.y);
Debug.Log("dashleft");
}
else if (Input.GetKeyDown(KeyCode.RightShift) && transform.localScale.x == 1 )
{
//dash right
rb.velocity = new Vector2( dashing, rb.velocity.y);
Debug.Log("dashright");
}
Comment
Your answer

Follow this Question
Related Questions
How to do dodge Unity2D like Crossing Souls or Hyper Light Drifter. 0 Answers
Cant get dash attack working 0 Answers
Homing attack suggestion 0 Answers
Speed boost button for player 0 Answers