How to prevent discrete camera jumps when re-parenting a node?

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

I have a Player which is aKinematicBody2D with a Camera2D. The camera has smoothing_enabled and is set up to smoothly scroll until the player is in the center of the screen.

When my player lands on horizontally moving platforms, I make him a child of the platforms so that he moves along with them.

Here is the code for that re-parenting:

# if player is on moving platform, reparent him to that platform

if current_platform != null:
	if not on_moving_platform:
		if == "MovingPlatform":
			var current_global_position = global_position
			var new_parent = Game.get_node("Level/MovingPlatform")
			global_position = current_global_position
			on_moving_platform = true


Unfortunately, whenever my player lands on such a platform, the Camera2D discretely cuts so that the player is centered in the screen. This is very ugly.

It seems that when you re-parent a node with a Camera2D, the Camera2D resets, ignoring its current scrolling position.

How can I avoid this so that landing on such platforms and re-parenting my Player does not cause discrete camera jumps?

:bust_in_silhouette: Reply From: jim

I’ve did something similar when trying to work with a 2 player game that uses the same screen for both characters: when one of the character dies, the camera would stop to focus on the Midpoint of both players coordinates to just follow one player.
What you need to do is create a camera that is a child of a Node2D that acts as its controller, and recieves the reference to whatever it needs to follow. Something like:

extends Node2D
var target

func set_target(_target):
    target = _target

func _process(delta):
    global_position = target.global_position

That way the Camera Controller node would instantly warp towards its new target, but the camera would always Lerp into position.

Hope that helps!

By the way, that would also help if you are trying to implement a ScreenShake effect, as you can see here:

jim | 2018-09-13 20:01

1 Like