write a godot player script to move in 4 2d directions with keyboard and is smooth and a…
- User
write a godot player script to move in 4 2d directions with keyboard and is smooth and a camera follow script to smooth follow it too
- Assistant
write a godot player script to move in 4 2d directions with keyboard and is smooth and a camera follow script to smooth follow it too
Here's how you can create a smooth 4-directional movement for a player in Godot, along with a camera that smoothly follows the player.
Attach this script to your CharacterBody2D (for Godot 4.x) or KinematicBody2D (for Godot 3.x) player node.
# Player.gd
extends CharacterBody2D # Use KinematicBody2D if you're using Godot 3.x
# Exported variable to adjust movement speed from the editor
@export var speed: float = 200.0
# Acceleration and deceleration for smooth movement
var acceleration: float = 1200.0
var deceleration: float = 1200.0
func _physics_process(delta):
# Get input direction
var input_vector = Vector2.ZERO
input_vector.x = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
input_vector.y = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
input_vector = input_vector.normalized()
# Calculate target velocity based on input
var target_velocity = input_vector * speed
# Smoothly interpolate velocity towards target velocity
if input_vector.length() > 0:
velocity = velocity.move_toward(target_velocity, acceleration * delta)
else:
# Decelerate to a stop when no input is given
velocity = velocity.move_toward(Vector2.ZERO, deceleration * delta)
# Move the character
move_and_slide()
Explanation:
Input Handling: We gather input for the four directions using built-in actions (ui_right, ui_left, ui_down, ui_up).
Normalization: The input vector is normalized to ensure consistent speed in all directions, even when moving diagonally.
Smooth Movement: We use move_toward to smoothly interpolate the player's velocity towards the target velocity, creating a smooth acceleration and deceleration effect.
Movement Execution: The move_and_slide() function is used to move the player while handling collision responses.
Attach this script to a Camera2D node in your scene.
# SmoothCamera.gd
extends Camera2D
# Exported variable to set the target node (player) from the editor
@export var target_path: NodePath
# Variables to adjust smoothing behavior
@export var smooth_speed: float = 5.0 # Adjust to control how quickly the camera follows
@export var offset: Vector2 = Vector2.ZERO # Offset from the target position, if desired
var target: Node2D
func _ready():
# Get the target node (player)
target = get_node_or_null(target_path)
# Enable built-in smoothing of Camera2D
smoothing_enabled = false # We'll handle smoothing manually in the script
func _process(delta):
if target:
# Calculate the target position with offset
var target_position = target.global_position + offset
# Smoothly interpolate the camera's position towards the target position
global_position = global_position.linear_interpolate(target_position, smooth_speed * delta)
Explanation:
Target Assignment: We use an exported NodePath variable to assign the player node as the target from the editor.
Smooth Following: Inside _process(delta), we interpolate the camera's position towards the player's position using linear_interpolate, which creates a smooth following effect.
Adjustable Smoothing: The smooth_speed variable controls how quickly the camera catches up to the player. A higher value means the camera follows more closely.
Offset (Optional): You can add an offset to the camera's position relative to the player if you want the camera to be slightly ahead or behind.
Ensure that you have the input actions set up in your project settings:
ui_up (e.g., Up arrow key, W key)ui_down (e.g., Down arrow key, S key)ui_left (e.g., Left arrow key, A key)ui_right (e.g., Right arrow key, D key)Add Player Node:
CharacterBody2D node (or KinematicBody2D for Godot 3.x) and name it Player.Player.gd script to this node.Sprite or any visual representation as a child so you can see the player.Add Camera Node:
Camera2D node to your scene (not as a child of the player).SmoothCamera.gd script to the Camera2D node.target_path to the Player node (you can drag the player node from the scene tree to this variable).Camera2D properties.Test the Scene:
Player Speed:
speed, acceleration, and deceleration variables in the Player.gd script to fine-tune how the player moves.Camera Smoothness:
smooth_speed variable in the SmoothCamera.gd script to change how tightly the camera follows the player.smooth_speed value makes the camera lag more, creating a looser follow effect.Godot Version: The scripts provided are intended for Godot 4.x. If you're using Godot 3.x, replace CharacterBody2D with KinematicBody2D and adjust the code accordingly.
Optimization: For performance, especially on lower-end devices, consider moving camera logic to _physics_process(delta) and adjusting interpolation methods as needed.
Further Enhancements:
With these scripts, you have a smooth 4-directional player movement and a camera that smoothly follows the player in your Godot 2D game. Adjust the variables to suit the feel of your game, and build upon this foundation for your project's needs.
Not yet included in a published release.