#Help with room boundaries restricting the camera

1 messages · Page 1 of 1 (latest)

thin magnet
#

I am making a 2d game where I want to have different rooms (kinda like a metroidvania). How would I set the camera boundaries and have it so the player can still move from room to room?

fathom galleon
#

you can have an Area2D that holds the room boundaries and camera position

#

so when the player enters it you can emit a signal to update the camera attributes

thin magnet
#

How would I go about doing that?

fathom galleon
#

create a script that extends Area2D and add some exported variables

#
class_name RoomArea
extends Area2D

@export var camera_position:Vector2
@export var camera_limit_left:float
@export var camera_limit_top:float
@export var camera_limit_right:float
@export var camera_limit_bottom:float

something like this

#

depending on how you setup this area you could use the area's collision shape size to calculate the camera limits

thin magnet
#

what does extends Area2D mean?

#

Is that because I would put the script into an area2D?

fathom galleon
#

yes

#

is an Area2D with some extras

thin magnet
#

Trying to get this to work rn

#

Do you know any good tutorials that can help me create something like this?

fathom galleon
#

is it something like this you want?

#

I have an area with a collision shape with the size of the room (that will always set the camera limits)

#

here is the room area script:

class_name RoomArea
extends Area2D

@onready var shape: RectangleShape2D = $CollisionShape.shape


func _ready() -> void:
    body_entered.connect(on_body_entered)

func on_body_entered(p_body:Character) -> void:
    if p_body is Character:
        var limit_left:int = int(global_position.x)
        var limit_top:int = int(global_position.y)
        var limit_right:int = int(global_position.x + shape.size.x)
        var limit_bottom:int = int(global_position.y + shape.size.y)
        Signals.room_entered.emit(limit_left, limit_top, limit_right, limit_bottom)
#

here is the camera script

class_name CameraController
extends Camera2D

const TRANSITION_TIME:float = 0.5

var tween:Tween

func _ready() -> void:
    Signals.room_entered.connect(on_room_entered)


func on_room_entered(p_limit_left:int, p_limit_top:int, p_limit_right:int, p_limit_bottom:int) -> void:
    if tween: tween.kill()
    tween = create_tween()
    tween.tween_property(self, "limit_left", p_limit_left, TRANSITION_TIME)
    tween.parallel().tween_property(self, "limit_top", p_limit_top, TRANSITION_TIME)
    tween.parallel().tween_property(self, "limit_right", p_limit_right, TRANSITION_TIME)
    tween.parallel().tween_property(self, "limit_bottom", p_limit_bottom, TRANSITION_TIME)
#

and the room_entered signal I added to an auto-loaded called Signals

#

that is all