thank you. i applied it with random function. and i got random velocities. But i dont get a smoothen change?

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

You can apply easen functions (ease) or linear interpolation (lerp) between old and new velocity, or limit the range for chosing the new velocity.

There are many built in math functions you can use and combine to get the result you want
docs.godotengine.org/en/stable/classes/class_@gdscript.html

Another option (maybe the best for soft motions) is to implement the uniformly accelerated motion you may know (v=v0+at), instead of modifying velocity, alter its acceleration and cap the max velocity.

:bust_in_silhouette: Reply From: Zylann

There are many ways to move an object smoothly. If the motion must be a bit randomized, then it should be code-driven at some point.
If you use velocity you can modify it through a random acceleration:

var randomness = 1.0 # higher is more random, lower is less
velocity += randomness * delta * Vector2(rand()*2-1, 0)
# Then cap the velocity so it doesn't gets out of hands
if velocity.length() > max_velocity:
    velocity = velocity.normalized() * max_velocity

There is a caveat though. Velocity being capped, it may “stick” at max or at min for small amounts of time because the randomness could try to increase it but could not.
This is where Perlin noise or Simplex noise algorithm would be handy, because it can generate “smooth” random values easily. Unfortunately this has been asked but not integrated yet into Godot.

So instead, what you can do is to use smoothing through lerp:

var velocity = Vector2()
var max_velocity = 100.0 # You can change it the way you like

var smooth_velocity = Vector2()

# The higher this value, the faster velocity will change. The lower, the slower. 0 means it cannot change.
var smooth_velocity_hardness = 1.0

func _process(delta):
	# Compute velocity the way you want, adding randomness and stuff
	velocity += Vector2(rand()*2-1, 0)
    if velocity.length() > max_velocity:
        velocity = velocity.normalized() * max_velocity

	# BUT
	# Instead of applying it directly, smooth it before and use the smooth velocity instead
	smooth_velocity = lerp(smooth_velocity, velocity, delta * smooth_velocity_hardness)

	set_pos(get_pos() + smooth_velocity * delta)

	# Note: here this technique is applied on the velocity, but you can also do it on the position,
	# or anything else, really. It just requires an intermediary variable and a lerp over time.

There is another way you can do what you want. If the motion is always the same regardless, and is just a bit “random” speed, then you could make an animation with AnimationPlayer, and simply adjust its play speed with a script, using the same technique as above to smooth the randomness.

For more examples about smoothing velocities, take a look at the 2d/lookat Godot demo project, in which you can see an arrow looking at the mouse in a smooth way.