Hey there, this is my first time posting anywhere for assistance with my code for my little test game. I have been scouring various forums and videos, trying to get a dashing mechanic working, however everything I have tried has not worked. What I would ideally like to happen is for the player to dash forwards x distance (over maybe 0.1 seconds) and then return to his normal speed, and be unable to dash again for roughly 1 second.
I would list off everything I have tried but I’ve been through so many forum postings and videos, that it is all jumbled together in my head. I have definitely tried moving the player character by attaching .AddForce to the rigidbody, by greatly increasing the player’s velocity when pressing the Input key I selected. I may have gotten the code wrong when trying these, as I was working from other people’s code trying to apply it to my own script.
The 2 images posted below contain all my code that I currently use to control my character. I had removed everything related to the dashing mechanics the last time I failed to get it working, so this is the bare-bones script I am currently using.
Should I be implementing the Dash mechanic in it’s own separate method and call it in the Update method?
Should I be attempting to move the Rigidbody itself, or just the Player’s position or greatly increase the speed it is moving when I press an input key?
Any help would be appreciated, I’m sorry if anything was unclear, I’m still very new to coding and am working on my understanding of Unity and C#.
Thanks!
How you design the dash mechanic is entirely up to you, I’d recommend seeing how other games have handled this mechanic, one such game that comes to mind is Rogue Legacy.

If I were doing it I’d do something like this.
using UnityEngine;
public class DashScript : MonoBehaviour
{
private const float dashSpeed = 3.0f;
private bool dashing = false;
private Vector3 startPosition;
private Vector3 endPosition;
[HideInInspector]
private Movement playerCharacter;
private void Awake()
{
InputManager.Dash += InputManager_Dash;
startPosition = Vector3.zero;
endPosition = Vector3.zero;
}
private void InputManager_Dash()
{
dashing = true;
startPosition = playerCharacter.transform.position;
endPosition = transform.position + ( Vector3.right * playerCharacter.DirectionFaced );
//DirectionFaced where 1 is right and left is -1
playerCharacter.StopRecievingInput ( );
}
private void Update()
{
if ( !dashing )
return;
else
{
//move the player character from the start position to the end position over dashSpeed time
transform.position = Vector3.MoveTowards ( transform.position , endPosition , dashSpeed );
//if the dash is over...
if ( playerCharacter.currentPosition == endPosition )
{
dashing = false;
playerCharacter.StartRecievingInput ( );
}
}
}
}
Hope it helps and best of luck.
1 Like
Hey, thanks for the reply and the example script. I tried it out but couldn’t figure out what was going on regarding the InputManager parts, they were causing errors in the script and I couldn’t find a way to work it into my code…
That being said I was messing around prior to your response and I have managed to get my dash mechanic sort of working, but it has some inconsistency. I have assigned “Space” to execute the dash code and set it on a timer. The timer will start and count down to 0 every time the key is pressed (if the timer is not already counting down from a previous key stroke), however the dash doesn’t always work. The dash itself where my character moves x distance only works about half the time, and will never work when my character is not already moving.
Below is my code showing what I have used to get these results, everything else has not changed from the previous 2 screenshots. Any thoughts on why I might be experiencing inconsistent results? I’m calling Dash() in the Update method, is there something I need to do in order to make it get called more reliably?
Ah, sorry mate. I should’ve explained, InputManager is a class I make in a lot of my projects. All input for the mouse and keyboard are checked here and if an input is detected it fires the event, which in your case is in the DashScript. It basically reduces the number of Update methods in various classes which I think increases performance.
//InputManager.cs, place this on a single GameObject in the scene.
using UnityEngine;
public class InputManager : MonoBehaviour
{
public delegate void KeyboardActionDash();
public static event KeyboardActionDash Dash;
private void Update()
{
if ( Input.anyKeyDown )
{
if ( Input.GetKeyDown ( KeyCode.Space ) )
{
if ( Dash != null )
{
Dash ( );
return;
}
}
}
}
}
1 Like
Oh I see now, I’m still learning some basic concepts so sorry if that should have been obvious. Understanding other people’s code at times can be challenging, but that clarified it and now I can hopefully apply this in the future. Thanks for all your help with this!