Sprites getting stuck together

Godot Version

Godot 4.2.1

Question

So, I’ve been following a tutorial online for a 2D top-down rpg and I have a simple, empty world scene with a player character and a skeleton. Everything is mostly working. The player moves, and animates correctly (I’m using AnimationTree and AnimationPlayer). The skeleton chases the player and animates correctly from idle to walking, changes direction, etc. The skeleton has a detection range (Area2D) and when it detects the player it moves towards it. The player is faster than the skeleton, so they can move out of range and return to be chased all over again…

The weirdness is when the player approaches the skeleton while facing north. That is: player is facing north, skeleton has detected the player and is facing south to chase them. All normal. But oddly, once they collide, the skeleton sticks to the player and then begins to move at the pace of the player? I can’t see that I’m doing anything different in either the skeleton script or the player script for the specific case of the skeleton moving south - I mean compared to the other three directions. And, just to repeat, this doesn’t happen in any of those other directions. I’m puzzled.

Things I’ve tried:

I hiked up the player speed and reduced the skeleton velocity to zero. I literally did this in the skeleton’s _physics_process function

velocity = Vector2.ZERO
move_and_slide()

And it still happens!

When I comment out move_and_slide() it doesn’t happen though.

Is there some way the skeleton’s velocity could be being changed by the framework?

It might help to add that although, obviously, I have added the skeleton scene to the world scene, I’m not instantiating the skeleton class anywhere else in my code.

I appreciate that I haven’t included enough code here for people to be able to solve this problem. I’m just wondering if anyone has seen anything similar?

1 Like

What kind of objects are the player and skelly? If you’re using physics based movement, physics things can happen.

They are both CharacterBody2D with rectangular CollisionShape2D. But… not using any gravity. I’ve enabled the debug feature to show collision shapes and there doesn’t look to be anything unexpected going on there.

I’ve just been doing some more testing. When the two Characters get bound together, the player can’t “push” the skeleton back - so movement can only happen south, east, or west. However, there is one other object in the world, a sign, a StaticBody2D, with it’s own CollisionShape2D. I can use this to “scrape” the skeleton off - by moving it against the sign and then moving the player left or right. Then the player can get distance, things revert to normal, the skeleton reenters chase mode, etc…

Here is my _physics_process for the skeleton. I’m using the vector angle instead of 2DBlendSpace, more as an exercise than for any other reason…

func _physics_process(delta):
	
	if player_detected == true:
		var player_position = $"../player".position
		var direction =  player_position - self.position
		direction = direction.normalized()
	
		#work out facing
		# first - little jiggle to remove negative angles
		var angle = fmod(direction.angle() + PI * 2, PI * 2)
			
		if angle >= PI / 4 and angle < PI * 3 / 4:
			facing = "down"
		elif angle >= PI * 3 / 4 and angle < PI * 5 / 4:
			facing = "left"
		elif angle >= PI * 5 / 4 and angle < PI * 7 / 4:
			facing = "up"
		else:
			facing = "right"
		
		velocity = direction * SPEED
		$animation.play("walk_%s" % facing)
		
		move_and_slide()

Thank you!

It was gravity. Changed Motion Mode from Grounded to Floating on the player and skeletons - things work. :slightly_smiling_face:

1 Like

There you go, I did it again, I solved the case. (???)