Making a KinematicBody2D move and shoot

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By DodoIta
:warning: Old Version Published before Godot 3 was released.

Hi!
I want a KinematicBody2D to shoot and move towards a point not at the same time. I tried this inside the _fixed_process callback updating the time elapsed using delta: firing works fine but calling the move function moves the sprite really fast, and I can’t seem to slow it down. This is the code I use for motion:

var movement = direction.normalized() * speed
move(movement)

where speed is set at 1.0. Decreasing it only reduces the distance walked, not its speed. Can anyone help me fix this behviour? It looks like a simple issue but I have few experience with Godot :slight_smile:

:bust_in_silhouette: Reply From: eons

Remember the linear movement equation?
∆s = V*∆t
In this case, the movement in a frame is ∆s and the time between frames named “delta” (a really low value) is ∆t.
Fixing your movement equation should be:

 movement = direction.normalized() * speed * delta

You normalize the directional vector because you want it to be always one unit long.

This way, you should have a “speed” displacement in “direction” when time sums 1.

I catually tried this but it kept giving me an error and I didn’t investigate. Turns out I wasn’t initializing the speed variable :stuck_out_tongue:
So… thanks, it works!

DodoIta | 2017-03-21 22:02