![]() ![]() You can open the resource and set its Extents to half the screen resolution, so the area covers one screen by default. To that end, add a RectangleShape2D to the CollsionShape2D’s Shape property. We want our anchors to be rectangular areas. Doing so makes the anchor detectable, but it doesn’t detect other physics bodies and areas itself. Set the Anchor2D’s Collision -> Layer to anchors only and turn off Monitoring and any Collision -> Mask. ![]() We also used a Sprite node to visualize the area’s bounds when running the game. The collision shape defines the anchor area. Let’s start with the anchor area as the detector and camera depend on it.Ĭreate a new scene with a root Area2D node named Anchor2D with a CollisionShape2D as its child. These layers make it easier to manage collisions as the AnchorDetector2D should only detect areas in the anchors layer. We set these three 2D physics layers in Project -> Project Settings -> 2D Physics: actors, obstacles, and anchors. AnchorDetector2D, is an area node we attach to the player to detect when it enters or exits an anchor area.When the player enters this Area2D, the camera’s target becomes this node’s location and zoom level. It smoothly follows the player by default unless the ship enters an anchor area. Although we keep it as a child of the player, we set it to be a top-level node, so it moves independently of its parent. ![]() AnchorCamera2D, the camera we attach to the player.We created three nodes to build the system: When entering and leaving specific areas, we change the camera’s target. The target can be the character the camera is attached to or any global position. In short, we have a camera that follows a target point using the arrive steering behavior, a vector math formula that makes an object smoothly move towards a target. Your browser does not support the video tag. Use steering behaviors to animate the camera’s zoom and position smoothly.Toggle anchoring the camera to the player or a fixed place when entering and leaving specific areas.Here we are telling the Tween to move the "position" of the camera, from the current value of camera.position to the current value of camera_target.position, in the span of seconds.In this tutorial, you will learn to make the camera dynamically switch between following a character and anchoring to a given location. Tween.interpolate_property(cam, "position", cam.position, cam_target.position, seconds) Which in this case I will go ahead and create from code instead of adding it in the editor: export var cam_path:NodePath Then in code use body.is_in_group("group_name") to check.Īnother thing you can do with this is to tween over time the camera position instead of changing it instantly. You can add groups to nodes in the Node panel on the Groups tab. Also, a common approach is to use groups to discern between the bodies. I'll also remind you that you can use the collision layers to filter what the Area2D detects. Var cam_target := get_node(cam_target_path) as Position2Dīy the way, you can use the same approach to teleport the player character. Var cam := get_node(cam_path) as Camera2D Well, add some Position2D nodes to mark the position for the camera, and then we can do this: export var cam_path:NodePath Now, you should be able to select the old and the new camera in the inspector panel.Īlright, but we don't want to change cameras, we want to move the camera. Something like this: export var old_cam_path:NodePathįunc when_body_entered(body:Node) -> void: And set current to false for the prior camera.Īlright, if you are going to have references to the cameras in your script, you may instead export some NodePaths for them. Then on the handler for "body_entered" signal of your Area2D you can set Camera.current = true for the Camera2D of the next room (areas of the map). ![]()
0 Comments
Leave a Reply. |