




So, we need Player‘s script to notify the rubber band when the left mouse button is released. When the player releases it, the rubber band must begin to apply a force to the character. The next step is to handle the release of the rubber band. If you try to drag the player now, you will see that the line now follows it.
#GODOT RIGIDBODY2D UPDATE#
The point of the line with index 0 is fixed in the starting position of Player to update the line we only need to update point 1, setting it to the relative position of the player. Var player_relative_position = player.position - position In the _process() function, we will update the rubber band line when on_slingshot is true: func _process(delta): Obviously, the on_slingshot variable is initially set to true because the character starts on the slingshot. So, let’s add this variable to the script: var on_slingshot = true Since we want the rubber band to follow the character only when it hasn’t been launched yet, we need a variable to know if the player is still on it. Onready var camera = $"./Player/Camera2D" And while we’re at it, let’s add a reference to the camera too, we’ll need it later. First, we need to insert a variable in the Slingshot.gd script that contains a reference to the Player node. When we move the character, we want the rubber band to update to follow him. Now, you can move the character around the screen by dragging it with the mouse: Now we just have to update, when drag_enabled is true, the position of Player, setting it to the location of the mouse cursor: func _physics_process(delta): Later we will see that, when the slingshot is released, Player will switch to Rigid mode and will only move based on the forces that are impressed on him and no longer in response to the player’s input. To do so, we need to use the _input_event() method: func _input_event(viewport, event, shape_idx):Īs you can see, we handle the input only when Player is operating in Static mode. We will change the value of this variable when we detect an input event related to the left mouse button. Let’s start by declaring a variable that we will use to store whether we are dragging the character or not: var drag_enabled = false These properties will be changed after launch to keep the player centered on the screen.Īttached to Player, you will find a script that we will modify during the tutorial.įirst, we want to be able to drag the character as if we were pulling on the rubber band of the slingshot. It’s initially set to follow the character only if it’s dragged off the screen (all drag margins are activated and set to 1). a Camera2D node to follow its movement once launched.a CollisionShape2D node with capsule shape (required for collisions).a Sprite node to display the player on the screen.The Player node properties are all set to the default values except for Pickable, which is enabled as we want the node to be draggable with the mouse. It’s the mode that we will use after the launch from the slingshot. It collides with other bodies and responds to forces applied to it. Rigid: the body behaves as a physical object.In this mode, we will be able to drag the player with the mouse without being subject to physics. Static: the body behaves like a StaticBody2D node.Instead, you apply forces to it (gravity, impulses, etc.), and Godot’s physics engine calculates the resulting movement, including collisions with other bodies and collision responses, such as bouncing, rotating, etc.Ī RigidBody2D node can operate in various modes in this tutorial, we will use two: This type of node implements simulated 2D physics. You can safely ignore the contents of this node. Environment: it contains the graphic elements of the game, little more than a few colored rectangles.
#GODOT RIGIDBODY2D CODE#
It’s just a reset button to restart the game (the reset code is already in Player‘s script)
