How To Dash In Unity
Вставка
- Опубліковано 7 вер 2024
- Learn how to add dashing to your game in Unity!
Source code: gist.github.co...
SOCIAL
Discord: / discord
itch.io: bendux.itch.io/
Twitter: / bendux_studios
SUPPORT
Buy Me a Coffee: www.buymeacoff...
MUSIC
The Thought of You by TrackTribe
3:41 , come on now no need for that I'm flustered.... your tutorials are amazing and you did a fantastic job explaining the process to us newcomers into the gaming development community!
I wasn't even thinking of adding Dash to my game, but after seeing this tutorial I quickly added it in next to no time. Thanks for the help! :)
also u deserve a lot more subs
This is such an easy way to add Dash. I didn't even want to try using dash but after I saw this video, I was able to implement it into my game in 5 mins. Thank you so much. Wish there was a lot more content from you.
Dude thank you so much, I decided to remove the lines of code that collect the gravity info which makes the player dash straight and also used the box collider I used to see if I am on the ground to determine if I could dash. This allows my dashes to still have gravity effects and to only be performed on the ground, so if the player rolls off the edge they will still fall.
Thanks so much mate
Thank you so much!
We were using Brackey's tutorial on 2D movement (and his script), but when it came to stuff like Dashing it was almost impossible to implement. Thank you!
These simple tutorials have been a lifesaver! THANK YOU!
For a top-down game, to dash towards the mouse pointer I used
mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
mousePos.z = 0f;
rb.velocity = dashingSpeed * Time.deltaTime * new Vector2(mousePos.x - playerUnit.transform.position.x, mousePos.y - playerUnit.transform.position.y).normalized;
The rest is pretty much the same. I removed the parts relating to gravity and I had to start the couroutine from FixedUpdate(), not Update(), otherwise I got inconsistent dash speed. Useful tutorial.
Thank you for sharing! As far as I know, you don't need to multiply by Time.deltaTime if you set the velocity as shown. Maybe I'm wrong, but that's how they do it in the documentation.
thanks for the tutorial, i made a more advanced version of it that includes vertical dahsing (both up and down) and diagonal dashing in any direction. i did it by replacing the vector(2) you set the rb.velocity with: new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")) * DashForce
Thank you for sharing!
@@bendux you are welcome
Hello, I tested your system but after a vertical dash, the player continues to climb, keeping the momentum of the dash until falling back with gravity. How can I fix this?
Woah, I've never seen an IEnumerator used this way before! Oh, all the possibilities this opens!
Thank you very much, your short videos are perfect for my procrastination periods. :)
Thanks for the tutorial.
Some of you may have a problem with the dashing as that it only allows you to dash in one direction. To fix this:
Change flip's private void to this:
private void Flip()
{
if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
{
Vector3 localScale = transform.localScale;
isFacingRight = !isFacingRight;
if(isFacingRight) localScale.x = 1f;
else localScale.x = -1f;
transform.localScale = localScale;
}
}
And the IEnumenaratorDash to this:
private IEnumerator Dash()
{
canDash = false;
isDashing = true;
float originalGravity = rb.gravityScale;
rb.gravityScale = 0f;
rb.velocity = new Vector2(transform.localScale.x * dashingPower, 0f);
print(rb.velocity);
tr.emitting = true;
yield return new WaitForSeconds(dashingTime);
tr.emitting = true;
rb.gravityScale = originalGravity;
isDashing = false;
yield return new WaitForSeconds(dashingCooldown);
canDash = true;
}
Hope this helps
Super useful video! Got it done in no time with no bugs! Really helped me out on my coursework, thanks! :)
You never mess up while typing. It's so smooth that I have the weird feeling that you make an AutoHotkey script or something in advance. Anyways, good video.
I actually use AutoHotkey to avoid mistakes. Thank you!
@@bendux Ok, you put that much effort? I'm suscribing.
@@pyxelgamer Haha, yes!
This is pretty aweome. Just throwing this out there, but helping out in the comments section and not on a Discord server can potientally help so many more people down the road imo
Thanks so much dude, I'm a complete noob to this and you made it look easy. I appreciate you.
Works perfectly! I had done my player turning a little differently so I just made if statements for isFacingRight true or false to dictate the different directions. Thank you so much!
if your dashing in the wrong direction try setting IsFacingRight to true in the player movement code, worked for me.
Thanks, it helped a lot. I just love how straight to the point it was.
really clean set up for the dash ability really similar to the one I have but
what I really liked was the trail feature. I didn´t knew about that really cool ;) !
This is the most useful video about dashing, thank you so much!
thankss
1000 sub dude keep going your videos are short and direct to the point the code is easy to understand Thank you for being here so glad i found ur channel
Thanks for awesome tutorial! Can you do how to wall jump in unity?
Yes! That would be awesome
I definitely want to make a video about wall jumping in the future.
@@bendux yo is out?? I just watched this.. hehe
Nice Tutorial and I tried it out directly bc i looked for a simple dash so long. I had a little Problem, when i dash my player gets boosted extremly far sometimes. I edited the code a little so i coul use my own Player Code (so it has stuff like Coins). But still a very good and simple tutorial, you definitly deserve more views!
Do note one thing though. Constantly starting a coroutine can compound the computation cost of your game. Make sure that you stop the coroutine after it is used.
In this case, it's not necessary to stop the coroutine manually.
Si quelqu'un a ajouté, comme moi, le wall jump avant ce tutoriel, le "dashing power", pour augmenter la vitesse en dash, n'est pas appliqué. Pour ça, j'ai du ajouter "&& !isDashing" au "FixedUpdate" :
if (!isWallJumping && !isDashing)
rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
Ainsi les deux fonctionnent !
Thank you for the videos and curious to see the future tutorials :D
Hey bendux, i am making a game for uni project. never used unity before, you have helped me alot. thanks
Thank you so much this was so helpful, I'm now beginning to program dashing up and down
We have a solution for that on our Discord server. Feel free to join!
@@bendux ok!
I don't usually comment on videos, but this realy helped me out! Thanks.
Always best tutorials in those underrated channels.. new sub ❤️❤️
Dude you're so funny and your tutorials are awesome.
you have the best tutorials for unity
Thanks a lot. I don't even know Dash code can be so easy.
Thank you for making dash so easy! I have implement dash into my game!
Thank you so much for the help! It's very helpful and short it's super worth subscribing.
Do you know how to make a character Dash forward on a 3D Side Scroller?
i know it's been a pretty long time since this was posted, but is there a way to cancel the dash mid-way through, via a jump or key input? this tutorial was really useful for someone learning the basics but i'm wondering if another method would be needed
Great video! Can't wait for your next video to be published!!
Thank You mans this helped a lot and now my cube can dash.
i would say increese the gravity when the player is done dashing or if the player is in air or not grounded such as jump or falling
Thank you so much!! I love this short simple and amazing tutorial!!
anybody know how to make the player look the other way with a different flip method.
How do you flip your player?
you don't know much this helped me. thank you so much!
this is very straight forward with a short intro, I love it! Also quick question, I would like to limit the number of dashes the player can have mid-air and when stepping on the floor it refreshes, thanks!
The answer to your question goes beyond the scope of a UA-cam comment, but I'd still like to help you. Join our Discord server, and let's solve your problem together!
Hi! I had a similar want, I used this fix, maybe it will work for you, in the private void Update()
I used the following lines of code,
if (IsGrounded())
{
canDash = true;
}
else
{
canDash = false;
}
simple but it works!
@@noobdev6464 It's hard to tell what's going on without seeing your script.
@@noobdev6464 I advise you to compare your script with the source code in the description.
@@noobdev6464 rb.velocity = new Vector2(transform.localScale.x * dashingPower, 0f);
2:00 what do i do if my character moves up,down,right and left??
We have a solution for that on our Discord server. Feel free to join!
hmm for some reason I only dash to the right and gravityScale never changes!
Edit: I've fixed it since I'm using this flip function:-
private void Flip()
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
I added:-
if(facingRight) rb.velocity = new Vector2(transform.position.x * dashingForce, 0f);
if (!facingRight) rb.velocity = new Vector2(-transform.position.x * dashingForce, 0f);
so my character will dash depending on facing right or lift variable, the full function would look like this:-
private IEnumerator Dash()
{
isDashAllowed = false;
isDashing = true;
float originalGravityScale = rb.gravityScale;
rb.gravityScale = 0f;
if(facingRight) rb.velocity = new Vector2(transform.position.x * dashingForce, 0f);
if (!facingRight) rb.velocity = new Vector2(-transform.position.x * dashingForce, 0f);
yield return new WaitForSeconds(dashingTime);
rb.gravityScale = originalGravityScale;
isDashing = false;
yield return new WaitForSeconds(dashingCD);
isDashAllowed = true;
}
and for the gravity scale I have no idea why its not changing at all, I'm still debugging.
Edit 2: i couldn't figure it out so i had to play around the issue using the dash animation. I had to add keys that change the GravityScale and disables the attacks scripts while dashing and resetting everything at the last keyframe.
It's so simple thank you very much 😁
you content is focused and neat! subbed!
Hey I have a Question. How do I make it so that the Player can also dash diagonally?
We have a solution for that on our Discord server. Feel free to join!
Hi my character sometimes dashes into objects and gets stuck, im using your movement with unity input system tutorial and this dash tutorial, any ideas on whats gone wrong?
Thank you bendux. The video helped me so much.
Hi! Great tutorial as always! Is it possible to make level system in Unity?) like you rich the end of a level and then you appears in new one. It would be pleasure to see it from you!)
I've added it to my list. Thank you for the suggestion!
I had a problem where my player only could've dashed in one direction (right), but my friend and I found a solution, here is my IEnumerator solution:
private IEnumerator Dash()
{
canDash = false;
isDashing = true;
float originalGravity = rb.gravityScale;
rb.gravityScale = 0f;
if (rb.velocity.x > 0)
{
rb.velocity = new Vector2(transform.localScale.x * dashingPower, 0f);
}
else if (rb.velocity.x < 0)
{
rb.velocity = new Vector2(transform.localScale.x * dashingPower * -1, 0f);
}
yield return new WaitForSeconds(dashingTime);
rb.gravityScale = originalGravity;
isDashing = false;
yield return new WaitForSeconds(dashingCooldown);
canDash = true;
}
I'm glad you were able to fix it. Thank you for sharing your solution!
Hello, this was a great easy to understand video. I only have one question and that is how to add a sound effect without you just flying forever until you die and then play the effect.
no wories, figured it out after playing around with the placement of dashSoundEffect.Play(); in my code
Awesome video! Short and to the point!
I had an idea on how I want to do a dash function but mine wasn't as nice as yours. =)
I hope you'll make more videos =)
Hi bendux.
First of all, congrat for your effort. I know manage a youtube channel requires time and will, so thank you.
I miss something about dashing in your video: usually, dash is used to dodge traps or enemies. How should I do to avoid collisions through the dash moment? If I disable the player collider he can walk through walls.
The answer to your question goes beyond the scope of a UA-cam comment, but I'd still like to help you. Join our Discord server, and let's solve your problem together!
you really need to be more popular then you are your videos are truly amazing you deserve to have your pillows cold and your gas prices lowered
Nice tutorial, I was thinking of making it so you can dash through enemies without losing velocity, how would i Impliment that?
Great and understandable video! Great job bendux
This was very helpful man!
I love these tutorials
Thanks for the video!! Can you explain how to put dash in input system?
We have a solution for that on our Discord server. Feel free to join!
i am using the new input system. how can i implement the dash using this new system?
Thanks for the tutorial man, but the problem I have is that the player's momentum doesn't reset afterwards so it keeps going forwards in that dashing speed
Would you like to show me your code on Discord?
@@bendux actually no, I figured it out myself by manually setting player velocity to zero when the dash ended, I just forgot to edit the comment lol
Thanks anyway
@@dachigobeja8490 I'm glad you were able to fix it.
BROTHER the dash is really slow for my player when i raise the dash power i make it longer not faster also the tail is under the player how i can fix this
Would you like to show me your code on Discord?
What about 3D Dash? Because 3D didnt have a gravityScale...
Thanks, it looks awesome!
I have a problem. That's when I dash, my character slides until it touches an obstacle. How to stop the character from slipping anymore. Thank you
It's hard to tell what's going on without seeing your script.
@@bendux ohh thank you, i fixed this
Hello, i followed your tutorial but when i tried to jump and dash my character just keeps floating until the end of the screen without faling down, can you help me with this ? Tysm!
nvm i forgot to move the trail renderer and now i got, thanks for the tutorial !
pls do more vids because they help VERY MUCH
Hello thank you very much for this tutorial really is very good! I have a question ! how can I make the dash work every 5 seconds? since every time I press the button makes the dash, thank you very much for your time!
Increase the dashing cooldown at the top of the script.
@@bendux hey i have one question. So the script works and i did the same as you and yeah trail shows up and all that but we don't actually dash. Like when we press shift it just goes in the speed i was already before dashing. Do you know how to fix this?
@@sirgwindortvinuel Would you like to show me your code on Discord?
Very helpful, thank you, subbed
Thank you for making dash so easy🔥🔥
and i have a problem why when i dashing, the gravity scale does not back to 0
Would you like to show me your code on Discord?
Complete beginner here, but I noticed that the player can dash while not moving. Here's my fix in case anyone else is struggling with this.
put this inside the Update method
canDash = false;
if (Input.GetAxisRaw("Horizontal") == 1 || Input.GetAxisRaw("Horizontal") == -1)
{
canDash = true;
}
Flip();
Hope this helps!
hey could you make an video combining the wall jump/slide and this vid cause i want to incorporate both but adding both codes to the single script is difficult with some of the differences between them as well as world settings for the gravity for these different projects cause i think im having issues with it
We have a solution for that on our Discord server. Feel free to join!
Help, my cube can only dash to the left! Can you give me some instructions so my cube can flip?
Would you like to show me your code on Discord?
I tried to make dashing in untiy allot but I couldn't add a cooldown because I'm new to codeing
Thank you and congratulations on 1k
Thank you!
Very easy to follow thanks so much here is a sub 👍
I am adding a Speedpad to my game, would this work but instead of pressing left shift key
Of course!
Nice job, it works perfectly except one thing :
Player dashes as expected but trail doesn't show up if the tiles back have an order in Layer > 0, whereas my player has an order in layer at 5
Don't know how to correct this
issue fixed : trail renderer has an order in layer parameter. Give it the desired value and all it works
Anyway, thanks for all your vids, helped me a lot ;)
thank you very much👍
GOD Tier Tutorial!
Amazing tutorial, the only problem I have is that my dash only works on one direction, could someone help me find out why?
Would you like to show me your code on Discord?
This tutorial is awesome - but I have one issue. I use a different code for my player movement, and this method doesn't seem to work with it at all. I'm not sure why, because there is nothing about Dash in my script. I'm wondering if its because there is no animation attached to the player?
Not sure how to fix this :(
Would you like to show me your code on Discord?
thanks man! Keep up the good work and take my follow
Thank you for the tutorial, but my dash only works when moving to the right direction but not the left direction. What can I do to enable both directions in Visual Studio?
How do you flip your player?
@@bendux I’ve copied the “private void Flip()” from the video but I think the game doesn’t know where it’s actually facing to flip.
Is it possible to make something similar but with “is moving” instead of “is facing?”
@@C4M3R0N03 You probably forgot to call the Flip method in the Update method.
With the part under…?
private void Update()
Flip();
@@C4M3R0N03 Yes!
Thanks for the video bro :D
Someone why if I stop the dash trail does not disapear? :c
If I continue moving disapear but so slowly. Thanks in advance.
Neverming haha I just activate "Apply ACTIVE color space" option xD
U r truly underrated man❤❤
Great vid! So easy too implement. How do you prefer to deal with wall collisions mid dash?
What do you mean?
@@bendux if you dash in mid air... then hit a wall half way through the dash... is it handled normally or do you have to consider a wall collision mid dash vs a jump. Im new to game dev... Does the dash action just stop at a collision or do you handle that differently during a dash?
@@Davearmstrong42 Unity automatically takes care of wall collisions.
Can you show us how to add a sound effect to the dash please?
Hey, good video, do you know how to do it on a topdown game to dash in the direction the player is moving (the y and x axis ) ?
Thanks for the help
We have a solution for that on our Discord server. Feel free to join!
I have a problem when i change the dash power nothing happens it doesn't want to be increased
It probably has something to do with the order of execution in your script.
@@bendux i fixed it
Great vid! can you also do a wall jump and crouching tutorial? thanks! also subscribed
Wall jumping and crouching are both on my list for future videos.
1:58 i use a differant flipping method/ so what do i do?
It depends on how you flip your player.
can someone help me it says 'Assets\Scripts\PlayerMovement.cs(59,13): error CS0246: The type or namespace name 'IEnumerator' could not be found (are you missing a using directive or an assembly reference?)' can anyone help
btw great tut
using System.Collections;
thx soo much
what if I had a top down shooter game what I could change with that code. and thanks you so much with this awesome tutorial
The answer to your question goes beyond the scope of a UA-cam comment, but I'd still like to help you. Join our Discord server, and let's solve your problem together!
hey a little advice please. error CS0246: The type or namespace name 'IEnumelator' could not be found (are you missing a using directive or an assembly reference?)
help pls
IEnumerator
Unity say that "IEnumerator" doesn't exists. How can I fix it?
using System.Collections;
Can i multiply the y axis of the rgbd2d with dashing power to dash diagonally ? This only allow us to dash horizontally
We have a solution for that on our Discord server. Feel free to join!
it wasnt working for me in my retro fps style game but i fixed it by just putting MoveSpeed = 15; then moveSpeed = 5; after DashingTime
I'm glad you were able to fix it. Thank you for sharing your solution!
my player kind of just stop a bit and the trail appears he doesnt get boosted forward, can anyone help?
did you fix it?
@@WHITEY_DEV yes