Hello !
I'm having trouble making my player slide at different position, I tried to use move_toward(), manualy add 1 to my player x postion but it's not working.
Here is my code :

extends CharacterBody2D

@onready var collision_shape_2d = $CollisionShape2D

signal player_moved()

func _process(delta):
	
	if Input.is_action_just_pressed("key_line_1"):
		position.x = 235
		position.y = 575
		player_moved.emit()
		
	if Input.is_action_just_pressed("key_line_2"):
		position.x = 405
		position.y = 575
		player_moved.emit()
		
	if Input.is_action_just_pressed("key_line_3"):
		position.x = 575
		position.y = 575
		player_moved.emit()
		
	if Input.is_action_just_pressed("key_line_4"):
		position.x = 745
		position.y = 575
		player_moved.emit()
		
	if Input.is_action_just_pressed("key_line_5"):
		position.x = 915
		position.y = 575
		player_moved.emit()

My game look like a rythm game where you just have to move at 5 places

Please help me ;-;

  • xyz replied to this.

    TheMikega Use a tween to animate the x position. Doing it with move_toward() is ok as well. It probably wasn't working because you had a bug in your code.

      xyz Thank you, it's working. I didn't know the existance of the tween function

      extends CharacterBody2D
      
      @onready var collision_shape_2d = $CollisionShape2D
      
      signal player_moved()
      
      func _process(delta):
      	
      	if Input.is_action_just_pressed("key_line_1"):
      		var pos_1 = Vector2(235,575)
      		var tween = create_tween()
      		tween.tween_property(self,"position",pos_1, 0.05)
      		#position.x = 235
      		#position.y = 575
      		player_moved.emit()
      		
      	if Input.is_action_just_pressed("key_line_2"):
      		var pos_2 = Vector2(405,575)
      		var tween = create_tween()
      		tween.tween_property(self,"position",pos_2, 0.05)
      		#position.x = 405
      		#position.y = 575
      		player_moved.emit()
      		
      	if Input.is_action_just_pressed("key_line_3"):
      		var pos_3 = Vector2(575,575)
      		var tween = create_tween()
      		tween.tween_property(self,"position",pos_3, 0.05)
      		#position.x = 575
      		#position.y = 575
      		player_moved.emit()
      		
      	if Input.is_action_just_pressed("key_line_4"):
      		var pos_4 = Vector2(745,575)
      		var tween = create_tween()
      		tween.tween_property(self,"position",pos_4, 0.05)
      		#position.x = 745
      		#position.y = 575
      		player_moved.emit()
      		
      	if Input.is_action_just_pressed("key_line_5"):
      		var pos_5 = Vector2(915,575)
      		var tween = create_tween()
      		tween.tween_property(self,"position",pos_5, 0.05)
      		#position.x = 915
      		#position.y = 575
      		player_moved.emit()

      But using move_toward() can't work cause I'm saying if Input just pressed it execute code for 1 frame, so it's making move_toward() work for only 1 frame.

      • xyz replied to this.

        TheMikega You should strive to minimize repetition in your code by using structured data to drive the execution. Your above code can be rewritten without excess repetition like this:

        extends CharacterBody2D
        signal player_moved
        var x_position = {"1" : 235, "2": 405, "3": 575, "4": 745, "5": 915} #dictionary that maps actions to x positions
        
        func _process(delta):
        	for action in x_position.keys():
        		if Input.is_action_just_pressed("key_line_" + action):
        			create_tween().tween_property(self, "position", Vector2(x_position[action], 575), .05)
        			player_moved.emit()

        Doing it with move_toward() is just a tiny bit more involved as it requires you to maintain the wanted position which is set whenever an action is pressed:

        extends CharacterBody2D
        signal player_moved
        var x_position = {"1" : 235, "2": 405, "3": 575, "4": 745, "5": 915}
        @onready var position_wanted = position
        
        func _process(delta):
        	for action in x_position.keys():
        		if Input.is_action_just_pressed("key_line_" + action):
        			position_wanted = Vector2(x_position[action], 575)
        			player_moved.emit()
        	
        	position = position.move_toward(position_wanted, delta * 2000)