Position not accurate

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

I’m implementing a magnet function, the code itself is working fine but the object is attracted to the front of player instead of the player itself. Normal position and global position results the same outcome.

The funny thing is, I did try to debug by comparing the global pos of player from the player node itself and the global pos obtained from the object is : The same. (What the actual f----

And just in case I also tried the global position of the sprite of the player. Its the same. So how in the name of christ did my player existing in front of everything?

BTW the problem is fixed by using a node 2d behind my player as the placeholder. Still, I need to find out why, I just dun have the time to do it myself cuz the deadline for the assignment is near…

Code just in case u need it:
for object side, where parea is the area obtained when entered the magnet radius

func _process(delta):
if Main.hp >= 1:
	if mag == false:
		var down = Vector2()
		down.y -= -1
		self.position += down * spd * delta
		ppos = parea.get_parent().get_node("put").get_global_position()
		position += (ppos - position)/10

How is your player scene set up? The position of the root node of a scene will generally be the intersection of the x and y axes, unless you modified its transform. If you have a sprite node that is offset from that then moving things to its position might put it in front of the sprite rather than what you would expect.

Here’s an example of a player from a game I’m working on where the sprite is positioned such that the position will be centred at its feet:

enter image description here

If I just add the sprite without correcting its transform the position will be centred, which ends up being to the front and at neck height:

enter image description here

It’s even worse if you just add the sprite and uncheck centered, because it then goes to the top left of the sprite.

So maybe there is some issue like that with your player scene?

HyperlinkYourHeart | 2023-06-18 14:36

Centered is not unchecked, with the transform of the root of player scene set at 0,0 (the orange cross is at the middle) so I don’t think the problem is in there…

But the scale is set to 4, is that affected?

alacz | 2023-06-18 14:44

If the sprite node is offset from the parent at all then the scale on the parent will multiply that offset, but the centre of the parent should still be obvious in the scene editor. I don’t know what effect the scale has on the position at runtime as I’ve never used it. Apparently the physics engine doesn’t support scaling properly and it’s not a good idea to set a scale on collision shapes or physics bodies, but even if it’s not one of those types of nodes maybe there are other effects.

HyperlinkYourHeart | 2023-06-18 15:19

So the scale might be the culprit huh.

Well, since I fixed this somehow, I’ll leave it be until someone smarter can get a definitive answer.

Thank you kind stranger, you have allowed me to let it go and sleep well.

alacz | 2023-06-18 15:39

:bust_in_silhouette: Reply From: SnapCracklins

Your sprites need to be children of the main body of the sprite. So your scene tree for the graphic should look like:

—> Shadow (modify z-index so it’s behind its parent)
—> Weapon

Then when you change anything with the body, its children will move in response with it. If a parent node moves, its children move with it. You just need to change your scene tree order.