- Home /
Question by
Novaero · Jun 22, 2020 at 03:00 PM ·
c#unity 2d2d-physicscharacter movement
How can I apply the dash movement? (Topdown 2D game)
I have a script for dash and movement which work separately, but how can I implement the dash as a LShift button press? (I'm a bit of a beginner)
public float moveSpeed = 5.0f;
private Rigidbody2D rb;
Vector2 movement;
public float dashSpeed;
private float dashTime;
public float startDashTime;
private int direction;
void Start(){
rb = GetComponent<Rigidbody2D>();
dashTime = startDashTime;
}
void FixedUpdate(){
rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
}
void Update(){
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
if(Input.GetKeyDown(KeyCode.LeftShift)){
Dash();
}
//Set Direction
if(Input.GetKeyDown("a")){
direction = 1;
}else if(Input.GetKeyDown("d")){
direction = 2;
}else if(Input.GetKeyDown("w")){
direction = 3;
}else if(Input.GetKeyDown("s")){
direction = 4;
}
}
void Dash(){
if(dashTime <= 0){
dashTime = startDashTime;
rb.velocity = Vector2.zero;
} else {
dashTime -= Time.deltaTime;
if(direction == 1){
rb.velocity = Vector2.left *dashSpeed;
} else if(direction == 2){
rb.velocity = Vector2.right *dashSpeed;
} else if(direction == 3){
rb.velocity = Vector2.up *dashSpeed;
} else if(direction == 4){
rb.velocity = Vector2.down *dashSpeed;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
how to stop player from automatically jumping on a moving platform 0 Answers
Multiple Cars not working 1 Answer
Unity 2018 2D Character Jump Glitch 1 Answer
Distribute terrain in zones 3 Answers