Making an Angry Birds thing side project, some funny glitch is happening

Question

Ok, so to diverse myself in other concepts of coding, I’m making a angry birds clone just to test my programing. So far, the bird will try to go towards the cursor. Funny thing, it works while testing it in the scene i made it, but if I place it in another scene and playing in that one, everything gets dislocated and funky.

extends StaticBody2D


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	var swing_pos : Vector2 = get_global_mouse_position()
	$Node2D.position = swing_pos
	#$RigidBody2D.position = $Node2D.position
	$Label.text = str(swing_pos)
	if Input.is_action_just_pressed("click"):
		$RigidBody2D.linear_velocity = swing_pos
		$RigidBody2D.gravity_scale = 0.5
		

This might be because you set the position of the node, not the global position. When you get the global mouse position, it is in reference to the root node, whereas when you set the position, you set it in reference to the parent. When you run it alone, this doesn’t cause a problem, as the parent and root are the same, leading to position and global position being the same as well.

To fix this, you could replace $Node2D.position = swing_pos with $Node2D.global_position = swing_pos.

TLDR; position is relative to the parent’s position, global is relative to the root node/(0,0), sometimes use global_position rather than position.

1 Like

Hmmm, I tried it out and it’s not working.

Though, the sprite that follows my cursor is now fallowing the mouse.
Oh wait, it’s working!
THX!