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