- Home /
My player keeps sliding after I use my dash, help!
So, I've made a dash script which allows me to dash in whichever direction I'm currently moving in, but the problem I now have is when I dash twice or perhaps three times, I start sliding which can't be stopped. I've tried to fix it but to no avail. Does anybody know how to fix this? Here's the dash script:
using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine;
public class Dash : MonoBehaviour { public Text CoolDownTimer; public int EnableDash; public float cooldown; public float resetcooldown = 0.3f; public float resetdash = 0.3f; public bool[] StopDash; public float DashSpeed;
public void Start()
{
DashSpeed = 10000f;
StopDash = new bool[4];
EnableDash = 1;
cooldown = 0.3f;
StopDash[0] = false;
StopDash[1] = false;
StopDash[2] = false;
StopDash[3] = false;
}
private void Update ()
{
if (Input.GetKey(KeyCode.W) && Input.GetKeyDown(KeyCode.LeftShift) && EnableDash == 1 && !StopDash[0])
{
GetComponent<Rigidbody>().AddForce(transform.TransformDirection(Vector3.forward) * DashSpeed);
StopDash[0] = true;
StartCoroutine(cooldownTimer());
EnableDash = 0;
}
if (Input.GetKey(KeyCode.A) && Input.GetKeyDown(KeyCode.LeftShift) && EnableDash == 1 && !StopDash[1])
{
GetComponent<Rigidbody>().AddForce(transform.TransformDirection(Vector3.left) * DashSpeed);
StopDash[1] = true;
StartCoroutine(cooldownTimer());
EnableDash = 0;
}
if (Input.GetKey(KeyCode.S) && Input.GetKeyDown(KeyCode.LeftShift) && EnableDash == 1 && !StopDash[2])
{
GetComponent<Rigidbody>().AddForce(transform.TransformDirection(Vector3.back) * DashSpeed);
StopDash[2] = true;
StartCoroutine(cooldownTimer());
EnableDash = 0;
}
if (Input.GetKey(KeyCode.D) && Input.GetKeyDown(KeyCode.LeftShift) && EnableDash == 1 && !StopDash[3])
{
GetComponent<Rigidbody>().AddForce(transform.TransformDirection(Vector3.right) * DashSpeed);
StopDash[3] = true;
StartCoroutine(cooldownTimer());
EnableDash = 0;
}
if(StopDash[0] || StopDash[1] || StopDash[2] || StopDash[3])
{
cooldown -= 1 * Time.deltaTime;
if(cooldown <= 0)
{
cooldown = 0;
}
}
CoolDownTimer.text = cooldown.ToString("F1");
}
IEnumerator cooldownTimer()
{
if (StopDash[0])
{
yield return new WaitForSeconds(0.2f);
GetComponent<Rigidbody>().AddForce(transform.TransformDirection(Vector3.back) * DashSpeed);
yield return new WaitForSeconds(resetdash);
StopDash[0] = false;
cooldown = resetcooldown;
EnableDash = 1;
}
if (StopDash[1])
{
yield return new WaitForSeconds(0.2f);
GetComponent<Rigidbody>().AddForce(transform.TransformDirection(Vector3.right) * DashSpeed);
yield return new WaitForSeconds(resetdash);
StopDash[1] = false;
cooldown = resetcooldown;
EnableDash = 1;
}
if (StopDash[2])
{
yield return new WaitForSeconds(0.2f);
GetComponent<Rigidbody>().AddForce(transform.TransformDirection(Vector3.forward) * DashSpeed);
yield return new WaitForSeconds(resetdash);
cooldown = resetcooldown;
StopDash[2] = false;
EnableDash = 1;
}
if (StopDash[3])
{
yield return new WaitForSeconds(0.2f);
GetComponent<Rigidbody>().AddForce(transform.TransformDirection(Vector3.left) * DashSpeed);
yield return new WaitForSeconds(resetdash);
cooldown = resetcooldown;
StopDash[3] = false;
EnableDash = 1;
}
}
}
You could set the rigidbody drag low/0 when dashing and high when not to stop it.
I'll do that and come back to you. Thanks for the help.
I just lowered the speed of the dash, I couldn't be bothered with more coding. I've already done enough, thanks for the help though.
Your answer
![](https://koobas.hobune.stream/wayback/20220612200819im_/https://answers.unity.com/themes/thub/images/avi.jpg)