- Home /
Cannot dash in left-down, left-up and right-down directions?
Alright, this is pretty much the first project I actually want to finish after a lot of my past attempts and I've been trying to fix this stupid bug. So basically I have a dash function on my player that allows you to dash when pressing space/jump and the dash works by multiplying the movement factor by an amount for the player to move faster. After you activate the dash you can move in every direction with no problems, but if you try to activate the dash while moving in the down-left, up-left or the down-right directions it doesn't work, but up-right does. I'm guessing it's because of the player moving in a negative direction but if that were the case it wouldn't work when moving straight left or down either. Any help would be appreciated as I do not understand why this won't work as intended. Thanks! This is the code for the movement: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
// Start is called before the first frame update
public float speed = 4f;
public float dashMult = 2f;
private float initialDashMult;
public float initialDashCooldown = 1f;
private float dashCooldown;
private float dashStopTime;
Rigidbody2D body;
float horizontal;
float vertical;
private void Start()
{
body = GetComponent<Rigidbody2D>();
initialDashMult = dashMult;
dashMult = 1f;
}
// Update is called once per frame
void Update()
{
horizontal = Input.GetAxisRaw("Horizontal");
vertical = Input.GetAxisRaw("Vertical");
if (dashCooldown > 0)
{
dashCooldown -= Time.deltaTime;
dashStopTime = initialDashCooldown - .2f;
if(dashCooldown > dashStopTime)
{
dashMult = initialDashMult;
}
else
{
dashMult = 1f;
}
}
//dash using space
if (Input.GetButton("Jump") && dashCooldown < .00001)
{
//start dash
dashCooldown = initialDashCooldown;
}
}
void FixedUpdate()
{
//movement
body.velocity = new Vector2(horizontal * speed * dashMult, vertical * speed * dashMult);
}
}
Your problem is not in the code you gave us, I ran your code and the dash worked in all 8 directions. The problem is either in another script conflicting with this one (Do you have any other scripts that access the same rigidbody), or in the input (Try changing keys for the axes).
To answer your solutions, there's not another script that accesses the same rigidbody and I tried dashing diagonally using WASD which surprisingly worked. (I tested using arrow keys before) Well seeing as you said that dashing is working for you I assume that it's my keyboard's fault, but at the same time it works for up-right so why shouldn't it work for the other ones? I suppose you tried using the WASD keys and concluded that it works. If you have time can you please try to also dash diagonally using the arrow keys if you didn't already? I want to know if it's my keyboard's fault or not. Anyway, thanks for the help, much appreciated!
Alright I figured it out; it turns out my keyboard is kinda broken, I have a 2-key ghosting if I try to use the arrow keys. Welp that's that. Thanks for letting me know and don't worry about this question anymore.
Answer by Core_9th · May 12, 2020 at 05:29 AM
Alright I figured it out; it turns out my keyboard is kinda broken, I have a 2-key ghosting if I try to use the arrow keys. Welp that's that. Anyone encountering this problem make sure you don't have ghosting on your keyboard; you can try every key for free here on this site: https://drakeirving.github.io/MultiKeyDisplay/ Have a good day to anyone finding this!
Wow what an unexpected answer. Glad you figured it out though!
Answer by DrewDunne · May 11, 2020 at 08:13 PM
So, I've gathered that initialDashCooldown is the "Cooldown" time for Dashing. But what is the difference between dashMult and initialDashMult? Does the dash decay over time? Perhaps more importantly, it seems you may have an issue with dashStopTime... it seems that it's value is simply 1.8f, which you are reassigning every frame via Line 34. If it's meant to have a constant value, I suppose that's fine but currently you are performing arithmetic every frame to yield a constant value.
It might be worth taking a look at those variables to see if there's something unintended occuring in your Update sequence that could be having the consequences you are seeing. If you can't find anything, it might be worth explaining what these variables are supposed to do and/or give more detail on how dash should function (for instance, does it decay or not). What is 'dashMult', etc.
Alright so I looked in the code just now and I can say this: 1. dash$$anonymous$$ult is a variable always multiplied to player speed and it can either be: 1 or initialDash$$anonymous$$ult (it's the value that dash$$anonymous$$ult gets set to when the player is dashing so the player is actually faster when dashing)
2.yeah I put dashStopTime there and it's bad so I should probably move it to start to avoid making a lot of operations for the same answer, that's what you intended to say right? Thanks for pointing it out!
Alright I figured it out; it turns out my keyboard is kinda broken, I have a 2-key ghosting if I try to use the arrow keys. Welp that's that. Thanks for letting me know and don't worry about this question anymore.