Area2D weird movement

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

Please watch this video and pay attention to the black collider that is following the player at the top of the screen.

That collider is the area2D and it acts as the deathzone in my game so whenever the player touches the upper edge of the camera, he/she dies. Everything is working as expected in the beginning, but as you could see in the video, the deathzone goes down more and more as the game progresses which is something I definetly don’t wan’t. At the end of the video the deathzone is far below it’s original position.

The deathzone is the child of the camera, and I have set up the camera so that it moves down slowly and if the player also goes down, the camera follows. This way the player can not get outside of the camera view by going down faster than the camera.

Here’s how that code looks:

	if player.global_position.y > camera.global_position.y:
	   camera.global_position.y = player.global_position.y

Seems like the deathzone moves down everytime that code is executed.

so how do you want the deathbox to move?, do you want the camera to move faster than the deathbox?, do you want the deathbox to move at a continuos speed?

umma | 2022-01-29 01:23

I want it to stay in one spot, which is at the top of the screen. Im trying to make it so that the player loses when the ball touches the upper edge of the camera. Right now, the deathzone moves down by itself gradually so that it is no longer at the top of the screen. This causes that the player loses prematurely because the player loses when the ball hasnt even touched the upper edge of the camera.

LyguN | 2022-01-29 10:12

:bust_in_silhouette: Reply From: Geazas

I think this is because of the smoothing_enabled property of the camera.

The camera position will not follow the exact position of the player due to smoothing, so is the Area2D which is the child of the camera
Maybe setting the camera’s process_mode to physics will help :confused:

I suggest attaching the VisibilityNotifier2D node to the player instead of using Area2D to notify when the player is out of the screen.

Yes turns out it was because of the smoothing on the camera. I decided to just turn it off. I tried using VisibilityNotifier2D but it didn’t work as it was supposed because it would kill the player either way before or after the player gets outside of the screen. Thanks anyway!

LyguN | 2022-02-04 21:54