I’m assuming you just want to give the node a bit of momentum.
To give a node momentum, you have to implement a system where the node (or the owner of the node) is aware of the node’s velocity. You’re essentially trying to create a primitive physics simulation.
Creating a basic physics system (for this)
Currently, you are moving your node based on the delta of your input (event.relative.y
). When no input is detected, the node’s position is not updated. For physically-based systems, the physical object (your node) must be continually updated because the state of the node describes how the node should move.
Examples of state variables in a physically-based system:
- Position
- Velocity
- Acceleration
- Angular velocity
- Center of mass (this is usually constant)
- …
As such, you need to implement a system that constantly updates the state of your node. Below is a simple implementation of such a system.
NOTE: The example is one-dimensional based on the code you supplied. If you wish the system to handle motion on more than one axes, you must correct the code below.
var acceleration: float
var speed: float # In higher dimensions, this would be named 'velocity'
var drag_coefficient = 1.0 # This controls how far the node "slides"
func _physics_process(delta):
# Acceleration
speed += acceleration * delta
# Drag
speed -= drag_coefficient * speed * delta
# Positional change
position += speed * delta
Because your use-case wants the speed (‘velocity’ in higher dimensions) to be controlled directly by your input, you may remove the #Acceleration
part - it’s not needed in this case.
To allow your drag to control the physical system, simply set the speed
to event.relative.y / delta
. Alternatively, you can use event.velocity
; it will avoid the need for delta
which isn’t directly accessible in _input()
.
How to get the physics delta outside _physics_process()
var delta = 1.0 / ProjectSettings.get_setting("physics/common/physics_ticks_per_second")
For smoother motion
If you want the system above to update at a higher rate, you may substitute _physics_process()
for _process()
. However, do note that the variable update rate of _process()
may introduce instability for systems like this that greatly depend on delta
. If such instability is experienced, please constrain the simulation to avoid unwanted states.