#Extreme newbie crying under her desk seriously needs some help with a dash mechanic

1 messages · Page 1 of 1 (latest)

proper pendant
#

Howdy everyone,

So, over the last two days I have been struggling to implement a dash mechanic in my fast paced FPS. No matter how many YouTube video tutorials, guides and forum posts I read, I can't seem to figure something that's working for me... so I was hoping you could help me <3.

Essentially what I am trying to achieve is that I want the player to be able to dash in whichever direction I am double tapping, for example:

  • Double tapping W would make the player dash forward relative to where they are aiming
  • Double tapping W while holding D would make the player dash 45 degrees to the right relative to where they are aiming
  • Double tapping D would make the player dash to the right relative to where they are aiming
  • You get the idea...

It sounds simple enough, but I just can't figure it out for the life of me. I should maybe have mentioned that I am not much of a programmer. In fact I am not a programmer at all, I am mostly a musician and I like to draw and write too. I am having a lot of fun learning Godot but it is also incredibly overwhelming. Mathematics are my kryptonite too so... there's that also, haha. I am so desperate to find a solution T^T.

Thank you in advance for your help =D!

trim musk
#

what do you have right now?

proper pendant
#

Right now I have a CharacterBody3D with a Node3D for the head as a child. Then as a child of the Head, I have a Camera3D.

I have been able to implement a double jump, a wall jump and some kind of headbob effect. It's all I have been able to achieve so far.

fickle siren
# proper pendant Howdy everyone, So, over the last two days I have been struggling to implement ...

I have to go soon, so I won't be able to walk you through it, but lemme outline the basic logic to be used
When the player presses any movement key, store it in a variable, and at the same time start a timer, this timer basically defines how quick you need to press the button again for it to count as a double tap. If the player presses a key from wasd, check if it's the same key as last time and that the timer hasn't run out yet. If both are true, then dash

#

Here's some pseudo code

func _input(event):
         if event is InputEventKey and event.is_pressed():
        if last_key == current_key and time_left > 0:
              dash()
        else:
               last_key = current_key
rancid sluice
#

I'll also add that you can use Input actions to get a single vector representing the motion direction: https://docs.godotengine.org/en/stable/classes/class_input.html#class-input-method-get-vector

For example, something like:

Input.get_vector("Left", "Right", "Up", "Down")

Assuming your actions are named the same as those string values, you can get a single vector indicating what combined movement input the player is currently inputting. Extremely helpful for controllers with analog inputs!

So if you detect a dash input, you could also get a single vector per the Input actions and use that direction to inform your dash 😄

proper pendant
#

Thank you, you two ! I am gonna try this and report back ! I truly appreciate your help =D !

proper pendant
#

So, thanks to your help, I have managed to implement the exact mechanic that I wanted to my game gdparty !!! BUT... I feel like my code might be very inefficient. Do you think it should be optimized ? Here is what I wrote :

var last_input_dir = Vector2.ZERO
var dash_window := 0.15
var dash_speed := speed * 20
var dashing := false
var dash_dir := Vector3.ZERO`



func _input(event):

    var input_dir = Input.get_vector("key_left", "key_right", "key_up", "key_down")
    
    if Input.is_action_just_pressed("key_up") or Input.is_action_just_pressed("key_down") or Input.is_action_just_pressed("key_left") or Input.is_action_just_pressed("key_right"):
        if dashing == false:
            if $dash_timer.time_left > 0:
                if input_dir == last_input_dir:
                    $dash_max_duration.start(0.20)
                    dash_dir = direction
                    dashing = true
            
            elif input_dir.x or input_dir.y != 0:
                last_input_dir = input_dir
                $dash_timer.start(dash_window)



func _physics_process(delta):

    if dashing == true :
        velocity.x = dash_dir.x * dash_speed
        velocity.z = dash_dir.z * dash_speed
    
    move_and_slide()



func _on_dash_timer_timeout() -> void:
    $dash_timer.stop()

func _on_dash_max_duration_timeout() -> void:
    $dash_max_duration.stop()
    dashing = false

Also, the gamefeel is off, like, I feel it lacks smoothness. I'd like it to ease out after instead of dramatically returning to normal speed 🤔 . I'll be toying with the script and see if I can make it better but if you have any suggestion, I would gladly hear it.

#

Also, how did you two color your code snippets ? I would like to do this too so it's a bit easier to read whenever I am sharing my code =).

rancid sluice
# proper pendant Also, how did you two color your code snippets ? I would like to do this too so ...

There's a few questions in there, so I'll start with the easy one. TBH I didn't know Discord supported this until Moranth used it above, so full credit to them. But, the code snippets support language hinting if you add a supported language right after the first triple set of backticks. So something like this:

If you want an easy way to get the examples above (like I did), you can just click the kebab menu (triple dots) next to a message and "Copy text". That will give you the raw message text, including the code snippet with the backticks and the language hint 😄

rancid sluice
# proper pendant So, thanks to your help, I have managed to implement the exact mechanic that I w...

I feel like my code might be very inefficient. Do you think it should be optimized ?

This partially depends on exactly what your goal is. If you're just wanting to optimize it for the sake of optimizing it, I wouldn't recommend spending the time to do so. I strongly recommend focusing on getting things working before you optimize too much - you can always optimize later. There is generally a tipping point where the right to optimize will come when the lack of optimization 1) didn't cost you much time up to that point, but 2) will start to cost you far more time in the future if left un-optimized.

That said, if you're optimizing towards a specific goal, then it may be worth doing so. If you're referring to optimizing the time of execution, you can get some vague time estimates for how fast the code runs with some print() statements and see if you can reduce it. If you're wanting to optimize for readability, there's a infinite number of ways to do so as it will depend on the person who is reading the code. Personally, I think you're code looks plenty fine! I'd have to really dig into some of the timer logic there to give any real critical feedback, but at a glance it looks good to me 😄

Also, FWIW, I promise you I have written FAR worse code than anything you have currently and I've been writing code professionally for 10+ years. Don't stress optimization too much, make cool stuff first.

rancid sluice
# proper pendant So, thanks to your help, I have managed to implement the exact mechanic that I w...

I feel it lacks smoothness. I'd like it to ease out after instead of dramatically returning to normal speed 🤔 . I'll be toying with the script and see if I can make it better but if you have any suggestion

Game feel is an immensely complex subject that will have no clear answers. If I were approaching the problem of smoothing out the movement, I would probably start by considering stuff like these:

  • Movement in "real life" is influenced by many factors that result in gradual changes over (often short) periods of time. You are effectively setting the velocity of the moving object with a semi static value - direction * speed. Velocity is the true "Physics" sense is influenced by acceleration, which is what gradually changes velocity. For example, gravity imparts an acceleration to all objects under it's influence which alters the velocity of those objects. Gravity doesn't cause your speed to snap to a particular number immediately, but applies a constant change to that speed over time. So you may want to consider implementing some acceleration to your movement to give it some speed-up and slow-down at the start and end of the dash respectively.

  • Godot supports a few different ways to automatically alter a value gradually over time. To start, you can research stuff like tween and lerp. These are tools/methodologies of changing a value over time, potentially with various factors applied throughout the time frame. So you could change velocity from value A to value B over X seconds - eg. change velocity from 0 to 5 over 0.3 seconds at the start of a dash, then 5 to 0 over 0.2 seconds at the end of a dash. (PS. This would effectively be a way of implementing acceleration, albeit a fairly simple and rigid example)

#

And if you have any questions after that wall of text, just let me know 😛 (though I must go eat lunch now 😋 )

fickle siren
# proper pendant So, thanks to your help, I have managed to implement the exact mechanic that I w...

Efficiency wise it is good enough, not something which needs reworking unless you specifically have problems with it
As for the gamefeel, you should look into lerping at tweening, both of which are methods which allows you to change a value gradually instead of instantly, they might help you get the ease out part. There are a ton more things which improve game feel though. One thing which I've seen fast paced games do is that they decrease ethe fov slightly during a dash or during increased speed, and sometimes even a little bit of blue around the screen edges, but you'll have to test to see if they work or not in your game

proper pendant
#

OMG, I am so grateful for your time Link of Origin. I cannot believe you would dedicate so much of your time to explain those things to me and I truly appreciate you ! I am absolutely going to look into tween and lerp. I had a very vague notion of what those were but I must admit that I haven taken the time to properly educate myself on the matter. So that's what I am going to do next. Hopefully tomorrow I can make it work !

I am focusing mainly on getting my character controller to feel great before I do anything else, so I'll be spending a lot of time on it so I can tweak the gamefeel just enough to where I am having a lot of fun just moving around, even without goals or enemies to defeat.

Have a good lunch and thank you again for your help, sincerely !

fickle siren
#

I'd just like to add in to the code colouring thing, you can do

To colour code any language supported by discord. Discord doesn't currently support gdscript, but swift gives the closest results, so I just use swift as a replacement

fickle siren
proper pendant
# fickle siren Efficiency wise it is good enough, not something which needs reworking unless yo...

You two are the best ! Thank you so much for your help Moranth, I am 100% gonna dive into lerp and tween tomorrow. I've learned so much with just that one question, I am so glad to see there are people like you two out there trying to help beginners. I am also glad that my code is good enough as it is. I have a bit of an imposter syndrome and I felt like maybe I was doing it wrong. But it works st I am still very proud !

Again, thank you so much, I genuinely appreciate your time !

fickle siren
# proper pendant You two are the best ! Thank you so much for your help Moranth, I am 100% gonna ...

No probs! Don't worry about the imposter syndrome, happens to everyone at some point
Usually you shouldn't spend time optimising code when you first write it, if it isn't causing any problems, since most of the time you'll automatically learn a better way to do stuff as you code more and return to your previous code to improve it yourself. Going out of your way to find optimisations for small stuff, especially as a beginner, usually leads to frustration. So just keep at it and you'll improve with time!
Anyways, good luck!