1. Attaching the Script
-In the Scene panel, right-click the inner player (CharacterBody2D) node.
-Click Attach Script (or click the script icon next to the node).
-Path: Set the path to res://scripts/player.gd.
-Click Create.
1. Attaching the Script
-In the Scene panel, right-click the inner player (CharacterBody2D) node.
-Click Attach Script (or click the script icon next to the node).
-Path: Set the path to res://scripts/player.gd.
-Click Create.
2. Clearing Boilerplate or Default Code
-Open res://scripts/player.gd in the Script editor.
-Delete all Default code inside red rectangle below line (extends CharacterBody2.)
-Save the file.
3. Copy and Paste variable code on your right in player.gd
-const SPEED = 200.0: Sets a fixed movement speed value for the player.
-var bullet_path = null: Holds the reference to the bullet scene (temporarily set to null until the bullet scene is created).
-var shoot_delay: float = 0.5: Sets a 0.5-second cooldown delay between each shot.
-var time: Timer = Timer.new(): Instantiates a timer object via code to handle the shooting cooldown interval.
-var can_shoot: bool = true: A true/false toggle that tracks whether the player is currently allowed to fire.
4. Add Player Movement Logic to player.gd. Copy and Paste code from you right.
_physics_process(delta): Godot's built-in physics processing function running every physics tick (60 FPS default).
movement(): Custom function isolating input checking and velocity assignment.
Input.is_action_pressed(...): Returns true continuously while the key is held down.
velocity.x = SPEED / -SPEED / 0: Sets horizontal movement vector right (+200), left (-200), or stops horizontal motion (0).
move_and_slide(): Moves the player using the velocity vector while handling physics collisions.
3. Now that our player is moving, we have a problem: the player can move off-screen. As you can see, we need to fix that.
-On movement() function add the code on your right under move_and_slide()
-How clamp(value, min, max) works: Prevents global_position.x from going below 16 (left screen padding) or above 1136 (right screen padding).
-Now your final movement() function code for player movement should look like the image on your right. Player can not go off-screen when test your game.