Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Josh_Browne · Nov 02, 2018 at 10:58 PM · 2d gamelimitdashground detection

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);

             }
         }
     }
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

107 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges