- Home /
How can I put a limit on "in-air dashes?"
I've created a little 2D project where my player only moves with short dash movements, which I've created using the script from this video: https://www.youtube.com/watch?v=w4YV8s9Wi3w What I want to do is create a way for my player to maintain the ability to dash directly upwards into the air, but I want the player to only have a limited number of dashes whilst they remain airborne. I've been trying to change parts of this video's (https://www.youtube.com/watch?v=QGDeafTx5ug) code to make it work with my existing 'dashMove' script, but I'm not proficient enough with coding to know what my next steps should be.
I should probably mention that the way I'm trying to limit the in-air dashes is with 'isGrounded' script from the second video (2D Double/Triple Jump) Any help would be greatly appreciated :)
My code:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class DashMove : MonoBehaviour {
private Rigidbody2D rb;
public float dashSpeed;
private float dashTime;
public float startDashTime;
private int direction;
public Animator animator;
// classes for testing if the player is touching the ground
private bool isGrounded;
public Transform groundCheck; // the actual ingame object that is attatched to the character.
public float checkRadius;
public LayerMask whatIsGround; // added a layer in unity called 'Ground' and put all my 'ground peices' on this layer
private int airMove; // in tutorial this was called 'extraJumps' but fot the purposes of my game, I changed it to better reflect the functionally I am trying to create
public int midAirValue;
// Use this for initialization
void Start () {
airMove = midAirValue;
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
dashTime = startDashTime;
}
// Update is called once per frame
void Update () {
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
if (isGrounded == true){
airMove = midAirValue;
}
if (Input.GetKeyDown(KeyCode.LeftArrow, KeyCode.RightArrow, KeyCode.UpArrow, KeyCode.DownArrow) && airMove > 0) // apparently 'GetKeyDown' needs 4 arguments, but as far as i'm aware, i've provided 4 arguments in the brackets, with the 4 KeyCodes...
{
rb.velocity = Vector2.up * dashSpeed; // these are the parts where I think i'm going pretty wrong. I'm pretty sure the 'Vector2.up' is completely wrong for what i'm trying to do, and I'm not 100% on whether 'dashSpeed' is the right class to use here either.
airMove--;
}
else if (Input.GetKeyDown(KeyCode.LeftArrow, KeyCode.RightArrow, KeyCode.UpArrow, KeyCode.DownArrow) && airMove == 0 && isGrounded == true) // apparently 'GetKeyDown' needs 4 arguments, but as far as i'm aware, i've provided 4 arguments in the brackets, with the 4 KeyCodes...
{
rb.velocity = Vector2.up * dashSpeed; // these are the parts where I think i'm going pretty wrong. I'm pretty sure the 'Vector2.up' is completely wrong for what i'm trying to do, and I'm not 100% on whether 'dashSpeed' is the right class to use here either.
}
if (direction == 0) {
if (Input.GetKeyDown(KeyCode.LeftArrow)) {
direction = 1;
} else if (Input.GetKeyDown(KeyCode.RightArrow)) {
direction = 2;
} else if (Input.GetKeyDown(KeyCode.UpArrow)) {
direction = 3;
} else if(Input.GetKeyDown(KeyCode.DownArrow)) {
direction = 4;
}
} else {
if(dashTime <= 0) {
direction = 0;
dashTime = startDashTime;
rb.velocity = Vector2.zero;
// reset animation bools ??
animator.SetBool("DashLeft", false);
animator.SetBool("DashRight", false);
animator.SetBool("DashUp", false);
animator.SetBool("DashDown", false);
}
else {
dashTime -= Time.deltaTime;
if(direction == 1){
rb.velocity = Vector2.left * dashSpeed;
animator.SetBool("DashLeft", true);
} else if(direction == 2){
rb.velocity = Vector2.right * dashSpeed;
animator.SetBool("DashRight", true);
} else if(direction == 3){
rb.velocity = Vector2.up * dashSpeed;
animator.SetBool("DashUp", true);
} else if(direction == 4){
rb.velocity = Vector2.down * dashSpeed;
animator.SetBool("DashDown", true);
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Can Dash only once 2D 2 Answers
How do I get my character to dash up? 0 Answers
How do i dash in a direction an arrow is showing? 1 Answer
I need help with my dash 0 Answers
2D Limit player speed 3 Answers