Making a node stay in front of another one (and ideally move around said node)

Godot Version

4.2.1

Question

so i’ve been trying to code for multiple days now something quite simple but for some reason i just can’t manage to make it work (atleast i do understand better now ig)

basically i’d just like for a piece of code to put the node "wand’ in front of the node “Player”
(here, the node wand is an instantiated child of player and player is instantiated to the level node, wich means that wand,player and level are all different scenes on their own but if thats not a smart thing to do i can still change it)
here’s the code i’ve come up with (does not return any error)

extends Node2D
@onready var player = $".."
@onready var wand = $"."
func _ready():
	pass
func _process(delta):
	physics()


func physics():
	if Input.is_action_just_pressed("ui_accept"): #debug
		wand.position = player.position + Vector2(10,0)

this code does make the wand move but VERY far away, much farther than i’d like it to be infact…


little screenshot to help understand
also, the wand would ideally move around the player to point where the mouse is but since i can’t do something as simple as putting it just in front of the player i’m not trying that yet…

i also forgot to mention but if i just put wand.position = global_position the result will be same if that helps

if anyone needs anything else to understand all of this better i’ll be happy to help !

You are mixing the wrong coordinate systems.

Based on your @onready variables you have set up your scene tree in the following way:

  • player
    • wand

So wand.position is relative to the player, but in your code wand.position = player.position + Vector2(10,0) you are adding the position of the payer a second time to the wand.

wand.position = Vector2(10,0) should do the trick.

1 Like

it works !
image

thank you very much, your explanation helps a lot too cause i never expected it to work like that

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.