Question regarding the move_toward functionality

Godot Version

4.2.1

Question

I decided to try some YouTube tutorials, to learn to build a game totally from scratch. Exactly at the point where the presenter taught me how to move the character everywhere efficiently, the code he provided looks like this:

if direction:
velocity.x = direction * SPEED
anim.play(“Run”)
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
anim.play(“Idle”)

This is just giving me the ability to move to the left and the right. I became quite bored, so I decided to mess around with the said code a little bit. Experiment on the velocity.x values a few times until I learned my character’s pace varied depending on the changes I made.

When my line is like this, it did a wee slide after it stopped moving:

velocity.x = move_toward(velocity.x, 0, -100)

Subsequently, I replaced the velocity.x and it looks like it was pulled by some magnet:

velocity.x = move_toward(100, 0, -100)

godot_pulled_to_the_right-ezgif.com-video-to-gif-converter

And then it went the other way after I made the value negative:

velocity.x = move_toward(-100, 0, -100)

While this is trivial, I really would like to understand the math behind it. I want to know how and why the numbers alone made a very significant difference given that the results are not what something I kind of expected (e.g. the character became faster, or slower). Who knows it can be super helpful to new developers like me to make something impressive out of it.

Thank you in advance.

Edit: Fixed the initial quote and improved the vocab

If you would like to understand a function, first thing is to check the documentation:

If that is not enough, you can always check the source code of the function:

2 Likes

here is an example you can throw into a sprite. the important part in the documentation is “Will not go past the final value”

extends Sprite2D
var m_min : Vector2 = Vector2(50,50)
var m_max : Vector2 = Vector2(400,200)
var m_state : int = 0
func _process(delta):
	#
	# figure out if we more moving towards min or max
	#
	if(0 == m_state):
		var v_distance_min = global_position.distance_to(m_min)
		var v_distance_max = global_position.distance_to(m_max)
		if(v_distance_min > v_distance_max):
			m_state = -1 # min is closer, move towards max instead
		else:
			m_state = 1 # max is closer, move towards min instead
	#
	# move towards min
	#
	elif (-1 == m_state):
		global_position = global_position.move_toward(m_min, 10)
		if(global_position.distance_to(m_min) < 1):
			m_state = 1; #move towards max
	#
	# move towards max
	#
	else:
		global_position = global_position.move_toward(m_max, 10)
		if(global_position.distance_to(m_max) < 1):
			m_state = -1; #move towards min

1 Like

Thank you. I read this before but still can’t figure out what it’s trying to convey. Did watched videos on the same topic but it’s futile. My lack of programming knowledge basis is to be blamed for this one.

So there is an alternate version on move_toward with the vector twist? Looks simpler than the former one. Probably will learn this at some point in one of those tutorials. Thanks tho.