func _physics_process(delta: float) -> void: # Every frame, we update the camera's zoom level and position. # Setting a node as top-level makes it move independently of its parent. Var _target_zoom : = 1.0 func _ready() -> void: If it's equal to `Vector2.ZERO`, # the camera doesn't have an anchor point and follows its owner. export var max_speed : = 2000.0 # Mass to slow down the camera's movement export var mass : = 2.0 var _velocity = Vector2. const SLOW_RADIUS : = 300.0 # Maximum speed in pixels per second. We set it to 960x540 as our project has a resolution of 1920x1080.Īttach a new script to Anchor2D with the following code:Įxtends Camera2D # Distance to the target in pixels below which the camera slows down. 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.Then make the Camera2D a child of the Node2D.In this tutorial, you will learn to make the camera dynamically switch between following a character and anchoring to a given location. You can add a Node2D and on its _physics_process you have it copy the global_position of your character to its own global_position, so that it follows it. Set your node_a to the character PhysicsBody2D, and your node_b to some RigidBody2D, and then the Camera2D can be a child of the RigidBody2D. Let us say you don't want to have something as child of your character PhysicsBody2D at all.And then you make your Camera2D a child that another Node2D. Instead, you can use a RemoteTransform2D that pushes its position (with update_position = true), but not its rotation (with update_rotation = false) to another Node2D. Well, there is no reason to make the Camera2D be a child of your PhysicsBody2D to make it follow it.Of course, if the rotation is driven by physics, then this option isn't helpful. That way the rotation does not affect the Camera2D. And the Camera2D is a child of the PhysicsBody2D. a KinematicBody2D) with a Sprite or similar. Presumably the player character is a PhysicsBody2D (e.g.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |