Path2d/PathFollow2d move between points of the Path2d

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By super312

I making a mario party game style, and wanted to know if there is a way to use the Path2d points we use to create the path as localization, or i will need to do a lot of math to move it.

The idea is, the player press the accept button, so it gives a random number that will be how many times it move from one point to the next one, is like thwroing a dice

:bust_in_silhouette: Reply From: Junton

I know this is sort of an old post, but I’ll answer anyway if somebody else finding this question from Google comes here.

I suggest that you make a Path2D going along with the game-board. Then you slide the Path-node’s offset so the moving character is on the point and write down what is the offset of every point in the game-board.

Then make variable, example “var board_point = 0”

then in _physics(delta) function: if board_point == 1: set_offset(input here the offset you wrote down from point 1), if board_point == 2: set_offset(input here the offset you wrote down from point 2) and so on…

Then you write the function for dice rolling. In the roll-function: board_point += dice_roll

For example: Player is in point 2 on game-board, you roll 6 → board-point=8 → player goes to offset which is indication on point 8.

Like this player instantly teleports to the point, so you can make extra-variable for counting down the dice-number and use timer to slow down the movement. And going between points you can use Tween-node to make the movement animated.

I made an example:

Scene-path:

  • Node2D

Sprite (game-board)

Path2D

PathFollow2D

Sprite (player)

PathFollow2D code:

extends PathFollow2D

var board_position = 1 #dice roll changes this
var Path_Offset = 0 #this is the offset which board_position puts it in _physics_process

func _physics_process(delta):
	if board_position == 1:
		Path_Offset = 0 #this is the first point and it is on 0 in the path
	if board_position == 2:
		Path_Offset = 438 # this is second point's offset
	if board_position == 3:
		Path_Offset = 860
	if board_position == 4:
		Path_Offset = 1252
	if board_position == 5:
		Path_Offset = 1590
	if board_position == 6:
		Path_Offset = 1845
	if board_position == 7:
		Path_Offset = 2172
	
	set_offset(Path_Offset)


	#This is just showing with pressing Enter/Return that the example works
	if Input.is_action_just_pressed("ui_accept"): 
		board_position += 1

Here is video of it working: