- Home /
Why isn't my code for dashing working?
My code is meant to register if the Player presses any of the horizontal keys twice in less than a second it will increase the Player's speed. Everything should work, I can't figure out how to properly format it (if that's the issue), I'm not the best at coding but I'm trying to learn. Thanks in advance for any help
if(Input.GetButtonDown("Horizontal"))
{
dashPress += 1;
while (dashTimer < 1)
{
dashTimer += 1;
System.Threading.Thread.Sleep(3000);
}
if (dashPress == 1 && dashTimer == 1)
{
dashPress = 0;
}
}
if(dashPress == 2)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal") * dashSpeed, moveDirection.y, Input.GetAxis("Vertical") * dashSpeed);
}
the problem to me, is that you suspend the entire thread that detect the dash, so for tree seconds this is unable to detect any further press
I suggest a diferent approach, register the press and use invoke to reset the counter at one second, some like
//global var
bool sliding = false;
...
//inside update
if(Input.GetButtonDown("Horizontal"))
{
if(dashPress == 0){
dashPress = 1;
Invoke("resetDashHold", 1.0f);
}
else
{
resetDashHold();
sliding = true
}
}
if(Input.GetButtonUp("Horizontal"))
{
sliding = false;
}
if(sliding)
{
moveDirection = new Vector3(Input.GetAxis("Horizontal") * dashSpeed, moveDirection.y, Input.GetAxis("Vertical") * dashSpeed);
}
and add a proper method to reset
void resetDashHold(){
dashPress = 0;
}
the behaviour is, tap
+ tap and hold
= dash
you cant do more than 1 dash per second and this can dash forever if you hold, to add a time limiter for the dash add a method that reset sliding
and invoke it (with the time) after set sliding
as true
Thanks so much, I'm only having one small issue with your fix and it's probably on my end. I'm getting this error: "Trying to Invoke method: PlayerController.resetDashHold couldn't be called."
weird since it has no parameters, try setting access to public
public void resetDashHold(){
dashPress = 0;
}
Answer by Fredow0001 · Apr 29, 2021 at 01:49 PM
Hey HolyHats!
It seems to me as if the problem is the first if statement!
When pressing the Button down, the value dashPress is increased by one, so i guess from 0 to 1. Then you check if the dashTimer is less than 1, which probably is true, so you increase the timer by 1 and wait for 3 seconds. Then you check if dash press is 1 (yes, because I just pressed the button) and the timer is 1 (yes, the while loop just increased it in one step to 1). Then you reset dashPress to zero, thus never moving the character faster!
Here is what you can do:
Take a look at Coroutines! It will help a lot. Also, try to avoid while Loops in the Update method, because it will try to run the while loop during the current frame. What you want to do is a timer, that lasts over several frames. You can achive this by starting a Coroutine:
IEnumerator Timer(){
while(dashTimer < 1){
dashTimer += Time.deltaTime; // increase by the time since the last frame
yield return null; // Wait for one drame
}
// The code after the while loop will be called when dashTimer >= 1
dashTimer = 0; //i.e. reset timer
}
You can call this coroutine using StartCoroutine(Timer()); whenever the button was pressed and the timer is not yet running. Then you can check if the currently runinng timer is less than 1.
void Update(){
if(Input.GetButtonDown("Horizontal")){
if(dashTimer <= 0 ){
StartCoroutine(Timer()); // Timer not running, start it now!
}
else{
//Timer running, the player pressed twice during 1 second
//your movement here
//reset timer after
StopCoroutine(Timer());
dashTimer = 0;
}
}
}
I hope that helps :P
Good luck, cheers!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do I make a game like Super Time Force? 1 Answer
Raw Image wont show in game view 1 Answer
Drawing a straight line between two points with 2D UI 1 Answer