Button to move character, positioning is for the wrong node?

Godot Version

4.5

Question

`so, I am trying to have a racing minigame where you pick your moves like a deckbuilder. So when you hit the first button it should move your character forward, and button 2 moves your character forward and to the left. The issue is that it moves the character to the coordinates relative to the center of the world, not relative to the character. so the buttons move the character to one specific spot. Also, when you open the scene it moves the character from the bottom of the screen to the center without any input.

Thank you!!! `

extends CharacterBody2D

Called when the node enters the scene tree for the first time.

var speed = 100

var node_y_position = position.y
var node_x_position = position.x
var new_position = Vector2(node_x_position, node_y_position - speed)
var hardleft = Vector2(node_x_position - .5speed, node_y_position - .5speed)
var hardright = Vector2(node_x_position + .5speed, node_y_position - .5speed)
var bigforward = Vector2(node_x_position, node_y_position - 1.5speed)
var smallleft = Vector2(node_x_position - .25
speed, node_y_position - speed)
var smallright = Vector2(node_x_position + .25*speed, node_y_position - speed)
var nomove = Vector2(node_x_position, node_y_position)
var target= position

func _ready() → void:
target=nomove
_physics_process(velocity)

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta: float) → void:
pass

func _on_button_pressed() → void:
target = new_position

func _physics_process(delta):
velocity = position.direction_to(target) * speed
if position.distance_to(target) > 10:
move_and_slide()

func _on_button_2_pressed() → void:
target = hardleft
_physics_process(velocity)

Unrelated to the issue, but _physics_process() is called automatically, you shouldn’t call it manually (and especially not give your velocity as argument for delta).


About your problem: All the variables are just set once at the beginning. So they only use the initial position and never change. Setting target to those constant values will result in the same target positions again and again.

The variables holding the positions should be independent from the player’s initial position (except target), like:

var new_position := Vector2(0.0, -speed)
var hardleft := Vector2(-0.5 * speed, 0.5 * -speed)
# etc.

And then, instead of setting target to those values, you should add them to it:

target += new_position

THANK YOU SO MUCH!
The buttons work beautifully now.

The character still moves forward to (0,0) without any input when i run the scene though. Any idea why that is happening?

If the initial values of target and position are identical, this shouldn’t be the case. You could try printing them in _ready() or _physics_process() to see if both of them are as intended.

	print("target: ", target)
	print("position: ", position)

Could any other script change one of them? Could it be the camera moving instead of the character?

Position was different than 0,0- I figured it out though! the parent node of the character was the scene root. I added a node2d as a parent of the character and set its position to the characters position, so I could set the characters position to 0,0. works great now!
Thank you!!!

1 Like