Node2D Global Position keeps shifting

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

Hey all. Loving Godot 3.1 and the C# stuff.

I’m writing a platformer (using MMX sprites to get everything feeling right for now) and am trying to do shooting. I instance a bullet and shoot it from my global position. However, my global position continues to shift and change the further I get from the origin, to the point where my shots instance inside of walls.

Here is a video of it:

Here’s the relevant code:

var shot = Assets.I.PLAYER_SHOT.Instance() as PlayerShot;			
var pos = this.BodyShape.GlobalPosition;
shot.GlobalPosition = pos;
shot.Direction = this.FacingRight ? 1 : -1;
this.GetParent().AddChild(shot);

I tried this.GlobalPosition as well, but no difference. My position gets farther and farther off the more I travel from (0,0) and then gets closer when I am closer. But, my collision boxes are still working and everything else is still interacting correctly. I tried calling this function before and after MoveAndSlide() with no difference in my position being noted (and the bullets still coming out of my feet or materializing in walls).

Any ideas on why my GP is so whacked?

Is your code included in the func _physics_process(delta) function? Does GlobalPosition update every frame or is it declared once and never again updated?
Normally you would have to shoot the bullets from the position of the player at every frame, so you need to update the position every frame.

johnygames | 2019-08-07 09:16

I am never updating GlobalPosition myself. GlobalPosition is a root property of Node2D. I always use “MoveAndSlide” in _PhysicsProcess. I was under the impression that GlobalPosition, as a root component of all 2d elements, is updated appropriately by the moving functions applied to it in "MoveAnd"Slide.

I AM trying to shoot the bullet from the position of the player when the shoot button is just pressed. But the GlobalPosition of my player is totally off. If you watch the video, you’ll see where my character is (which is correct), and when I shoot my bullets come out of my foot. Once I move around and fall down the pit, my “GlobalPosition” is even farther away in the side of a wall and my bullets come out even MORE away from my character. THe farther I move away from the origin of the scene (the top-left 0,0) the more skewed my GP becomes from my character.

fngreg7 | 2019-08-07 14:02

I remember having an issue with weird offsets and all I had to do to fix it was make a separate scene for my player (let’s name it Player.tscn). I left the origin as is in Player.tscn and then I added it to the main scene, where I controlled its movement via script.

It might also help to stop using GlobalPosition as a reference. try using the position property and offset the bullet by a certain amount on the x axis. I don’t use C#, but I guess it’s the same.

johnygames | 2019-08-07 16:01

Unfortunately, I have that. :frowning: . My player is in its own .tscn and I added it as a Node to my World scene.

Additionally, position is same as global position in my log testing. Even if I set an offset from global position, global position continues to get farther from the actual position of my node as I move more. I wonder if there is a C# bug here about position.

However, I had another idea. I am using “pixel snap” to get that 2d look. Is it possible that the float is floor/ceiling and creating a rolling msimatch the more it calculates?

fngreg7 | 2019-08-07 16:14

You could try it, but I don’t thik it matters. I’ve tried the following in GDScript and it works as expected. Try it too and report back whether position updates properly. Plece this in your Player script:

var projectile = preload("res://Scenes/Bullet.tscn")

func _process(delta):

	if Input.is_action_just_released("ui_up"):
		var bullet = projectile.instance() 
		bullet.position = position
		get_parent().add_child(bullet)

johnygames | 2019-08-07 17:03

I agree the code should work, it’s relatively standard code. My code looks really similar to yours, but something is happening where “this.position” is not equal to where I physically am on screen (as evidenced by the collision shapes that are turned on in the video), and that discrepancy between where I am vs where position says I am is dictated by how far away from origin I am.

Major differences in my code than what you provided though is that I am using Physics Process, not Process. Also I am moving my character via MoveAndSlide() which seems to be creating that GlobalPosition / Position being incorrect at a rate concurrent with how far I move off of origin.

Thanks for taking the time to help here, but I think that something else may be going on that is creating this positional drift…

fngreg7 | 2019-08-07 20:02

I also remember having offset issues when changing rotation/ direction. You might want to see into that as well, since you change the direction the object is facing. Sometimes changing direction messes up your position as well, but I cannot explain why that is. In any case I would suggest you remove the line which is responsible for changing direction and just keep your move_and_slide method. Maybe this gives you a hint as to what is at fault here.

johnygames | 2019-08-07 22:27