Folks, I'm new to programming but I'm really getting into the swing of it.

I have an idea I want to implement, but I'm having trouble with the methods I have tried.

Essentially what I want to do, is have my player walk over an area 2D trigger that will pan the camera to a target node's position, and then back to show the player an objective. I'd like my trigger and position node to be reusable in different levels.

My game is a simple 2D platformer. Can anyone suggest any methods to try?

Many thanks!

  • You can do it with an AnimationPlayer, but it's not very flexible, since you have to hand set the keyframes. Most likely you want to use a Tween. You can create a Tween that will control the Camera (it's just another node like anything else).

    When the player walks over the trigger, the trigger should have an export variable to set the position (or list of points) that the camera should move to. You can either just input a Vector2, or create some sort of dummy node (which could be a Position2D) that you can place manually in the editor.

    Then when the trigger happens you set the wait time of the Tween, and do what is called an interpolate property, which will move the camera. The code looks something like this:

    var new_position = Vector2(123.0, 456.0) # get this from the trigger
    your_tween.interpolate_property(camera_node, "position", camera_node.position, new_position, 3.0, Tween.TRANS_LINEAR, Tween.EASE_IN)

    The last three parameters are the time it takes to move, and the interpolation speeds and easing.

You can do it with an AnimationPlayer, but it's not very flexible, since you have to hand set the keyframes. Most likely you want to use a Tween. You can create a Tween that will control the Camera (it's just another node like anything else).

When the player walks over the trigger, the trigger should have an export variable to set the position (or list of points) that the camera should move to. You can either just input a Vector2, or create some sort of dummy node (which could be a Position2D) that you can place manually in the editor.

Then when the trigger happens you set the wait time of the Tween, and do what is called an interpolate property, which will move the camera. The code looks something like this:

var new_position = Vector2(123.0, 456.0) # get this from the trigger
your_tween.interpolate_property(camera_node, "position", camera_node.position, new_position, 3.0, Tween.TRANS_LINEAR, Tween.EASE_IN)

The last three parameters are the time it takes to move, and the interpolation speeds and easing.

    cybereality Wow, thanks for the speedy response. I should be able to try this at some point today. Thanks again!

    cybereality

    Thank you so much for your help. This is exactly what I needed. I have learnt a lot about tweens now, and I think I'll find them very useful for multiple things.

    I have a tween node saved as a scene, with a trigger, and position 2d. I can just add this to my camera on a level where I need the cut scene, make children editable, place my trigger and target position and voila. I also added export variables to control the time taken, and whether the action is repeatable.

    extends Camera2D
    
    export var time_taken = 3
    export var repeatable = false
    onready var tweenNode = $Tween
    onready var target_pos = $Tween/CutSceneTargetPos
    onready var trigger = $Tween/CutSceneTrigger
    
    func _on_CutSceneTrigger_body_entered(body):
    	cutscene_camera_tween()
    	if repeatable == false:
    		trigger.set_deferred("monitoring", false)
    	
    func cutscene_camera_tween():
    	tweenNode.interpolate_property(self,"offset", global_position, target_pos.position,time_taken,Tween.TRANS_BACK,Tween.EASE_OUT)
    	tweenNode.start()

    Actually this is sort of working, but the target position when used seems way off in game. Maybe this is down to me using "offset"?

    I don't think you want to use offset. You want to change the position directly.

    Yea I have tried that, but I couldn't get it to work. I'm having better luck by moving towards target position - self.position. I'll keep playing with it!

    I'm finding the issue maybe that I'm using a remote transform, to control the camera from the player...

    The camera should be the level on the same layer as the player (not as a child of the player). It helps if all your objects are in the same coordinate space (at least the dynamic objects like the player, camera, enemies, etc.). Otherwise things get confusing when the coordinates don't match up.