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 /
  • Help Room /
avatar image
0
Question by chocolatehog · May 25, 2017 at 01:14 AM · movementbuginfinitedash

Need help with dash ability

So I'm trying to make a 2D fighter and basically what I'm trying to do is give my character a simple dash ability where while running they can press the a attack button to dash forward a bit. The problem is that after a few dashes my stamina variable will go up infinity and i don't know why. Here is the character code I'm using. Its a mess but any help would be much appreciated:

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class BlueControls : MonoBehaviour {

 [HideInInspector] public bool facingLeft = true;

 public Text dashCheck;
 public bool dashing = false;
 public float dashMultiplier = 8;
 public bool canDash = true;
 public float dashTime = 0.5f;
 public float dashRegen = 2f;
 private int stamina = 2;
 public bool jump = false;
 public float moveForce = 365f;
 public float maxSpeed = 5f;
 public float jumpForce = 1000f;
 public Transform GroundCheck;

 private bool grounded = true;
 private Animator anim;
 private Rigidbody2D rb2d;

 void Awake () {
     anim = GetComponent<Animator> ();
     rb2d = GetComponent<Rigidbody2D> ();

 }


 void Update () {
     dashCheck.text = ("Stamina = " + stamina);
     grounded = Physics2D.Linecast (transform.position, GroundCheck.position, 1 << LayerMask.NameToLayer ("Ground"));
     if (Input.GetButtonDown ("Blue Jump") && grounded) {
         jump = true;
     }
     if (Input.GetButtonDown ("Blue Fire1") && !Input.GetButton("Blue Horizontal")){
         anim.SetTrigger ("Basic Attack");
     }
     if (Input.GetButtonDown ("Blue Fire2") && !Input.GetButton("Blue Horizontal")) {
         anim.SetTrigger ("Heavy Attack");
     }
     if (Input.GetButton ("Blue Fire3") && !Input.GetButton("Blue Horizontal")) {
         anim.SetBool ("Block", true);
     } else {
         anim.SetBool ("Block", false);
     }
 }
 void FixedUpdate(){
     float h = Input.GetAxis ("Blue Horizontal");
     anim.SetFloat("Speed", Mathf.Abs(h));
     if (h * rb2d.velocity.x < maxSpeed && !Input.GetButton ("Blue Fire2") && !Input.GetButton ("Blue Fire3")) {
         rb2d.AddForce (Vector2.right * h * moveForce);
     }
     if (Input.GetButtonDown("Blue Fire1") && !Input.GetButton("Blue Fire2") && !Input.GetButton("Blue Fire3") && stamina > 0 && canDash){
         rb2d.AddForce (Vector2.right * h * moveForce *dashMultiplier);
         dashing = true; 
         StartCoroutine("dtdt");
         anim.SetTrigger ("Dash Attack");
     }
     if (stamina == 0) {
         StopCoroutine ("dtdt");
         StartCoroutine ("dtdf");
         dashing = false;
         canDash = false;
     }
     if (stamina == 2) {
         StopCoroutine ("dtdf");
         canDash = true;
     }
     if (Mathf.Abs (rb2d.velocity.x) > maxSpeed && dashing == false ){
         rb2d.velocity = new Vector2 (Mathf.Sign (rb2d.velocity.x) * maxSpeed, rb2d.velocity.y);
     }
     if (h < 0 && !facingLeft){
         Flip ();
     }
     else if (h > 0 && facingLeft){
         Flip ();
     }
     if (jump) {
         anim.SetTrigger ("Jump");
         rb2d.AddForce (new Vector2 (0f, jumpForce));
         jump = false;
     }
     if (grounded) {
         anim.SetBool ("landed", true);
         } else {
         anim.SetBool ("landed", false);
     }
 }


 void Flip (){
     facingLeft = !facingLeft;
     Vector3 theScale = transform.localScale;
     theScale.x *= -1;
     transform.localScale = theScale;
 }
 IEnumerator dtdt(){
     while (true) {
         yield return new WaitForSeconds (dashTime);
         stamina -=2;
     }
 }
 IEnumerator dtdf(){
     while (true) {
         yield return new WaitForSeconds (dashRegen);
         stamina += 1;
     }
 }

}

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

130 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 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

Keyboard input not working for starter FPS game 0 Answers

Player keeps bumping when walking over to another sprite 1 Answer

Why does my player move without any buttons being pressed? 1 Answer

Can anyone help me with the dash please? 0 Answers

Using "AddForce" with a Character Controller 1 Answer


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