Change what object the camera's following using AnimationPlayer

Godot Version

Godot 4.2.2

Question

How do I change the object the camera’s following using the AnimationPlayer node?
So, here’s my setup:
image
I use the CutsceneManager to animate and change various properties for a cutscene. When the player enters the “Cutscene” Area2D, it will play the animation. And because I want the camera to be static in the meantime, I made this code on the Camera node:

extends Camera2D

@onready var player = $"../Player"
@export var follow: Node2D

func _ready():
	follow = player

func _process(delta):
	position = follow.position

And so, in the AnimationPlayer, I set-up keyframes to change this variable to follow a specific point, like so:
image
The problem is: when I change this value to follow the player again, instead of following it, the camera just stays still at the player’s initial position when it entered the scene.

Does anyone have a fix or any suggestions on how to approach this better?
Thank you!

I believe a good way of solving this is instead of changing wht the camera is following, change what object is camera the child of. So, at the start, for example, the camera is a child of the player - aka follows player, but when you want it to follow a different object, you:

other_object.add_child(camera)

But this would create a new camera, wouldn’t it?
Isn’t there a way to change the camera’s parent to the different object?

You only create a new object with instantiate() or new() calls etc.

Removing a child from one node and adding it onto another won’t spawn a new one, it just rehomes it~

Oh one thing to note though, I believe removing it from the tree with nodename.remove_child() will remove the camera from* the viewport or swap to another in the tree if you have another one enabled, so set it back to this camera after adding it as a child on the new parent call cameraname.make_current() on it!

I believe this would just replace the location of the camera, have tou tried it?

Oh, I’m so sorry!
I did, but for some reason the camera just… doesn’t change
image

@onready var camera = %Camera

func add_camera_child():
	add_child(camera)

And I also made this function for the player. But the camera just keeps following the player… Am I doing something wrong?

Got it, I was just figuring this in my head, but I think you should use: remove_child() before add_child(), basically remove from current parent and add to the new one. Tell me how it goes :slight_smile:

1 Like

You should probably create a new animation player for the camera separately. Because it just refers to the default transform properties. Without changing the position. There nothing else that seems wrong there.
It’s also referring to the first frame of the animation, the track time of 00:00 where the transform properties are default to player initial position. It looses the camera signature after reloading. And, just goes back to frame one. The would probably be some temporary locations of the camera track. That you would be able to rewind and forward to. Where the camera would be just stalling on the previous camera locations. Or, not.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.