+1 vote

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 current_platform.name == "MovingPlatform":
            var current_global_position = global_position
            var new_parent = Game.get_node("Level/MovingPlatform")
            get_parent().remove_child(self)
            new_parent.add_child(self)
            global_position = current_global_position
            on_moving_platform = true

else:
    ...

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?

in Engine by (1,571 points)

1 Answer

+1 vote

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 (33 points)

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

https://www.youtube.com/watch?v=DhHMS5FAZdc

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.