Im using Godot 4.x
So i ran into a problem where the wasd keys where moving forward, left, right, and backward but they always move like that. Example: If i move turn left, w wont make my character (me) go left (forward). Can you help? Sense I ChatGPT wrote these scripts in a specific way, I don’t really know how to edit them to where it doesn’t break anything.
MOVE SCRIPT:
extends CharacterBody3D
@export var speed: float = 10.0 # Movement speed
@export var jump_power: float = 5.0 # Jump power
@export var gravity: float = 9.8 # Gravity strength
# Reference to the camera node (adjust the path as necessary)
@onready var camera = $Camera3D
func _physics_process(delta):
var direction = Vector3()
# Get input for movement
if Input.is_action_pressed("move_forward"): # W key
direction -= camera.transform.basis.z # Use the camera's forward direction
if Input.is_action_pressed("move_backward"): # S key
direction += camera.transform.basis.z # Use the camera's backward direction
if Input.is_action_pressed("move_left"): # A key
direction -= camera.transform.basis.x # Use the camera's left direction
if Input.is_action_pressed("move_right"): # D key
direction += camera.transform.basis.x # Use the camera's right direction
# Normalize the direction vector and scale by speed
direction = direction.normalized() * speed
# Apply gravity when not on the floor
if not is_on_floor():
velocity.y -= gravity * delta
else:
# Jump logic when pressing the jump action and the character is on the floor
if Input.is_action_just_pressed("Jump"):
velocity.y = jump_power # Apply the jump power
# Apply horizontal movement
velocity.x = direction.x
velocity.z = direction.z
# Move and slide with the adjusted velocity
move_and_slide()
CAMERA SCRIPT:
extends Camera3D
@export var mouse_sensitivity: float = 0.2 # Mouse sensitivity
var pressed_esc: int = 0 # Tracks whether the ESC key toggled mouse lock/unlock
# Rotation variables for mouse look
var pitch: float = 0.0 # X-axis (vertical rotation)
var yaw: float = 0.0 # Y-axis (horizontal rotation)
func _ready():
# Lock the mouse cursor to the screen initially
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _input(event):
# Handle mouse lock/unlock with the UnlockMM action
if Input.is_action_just_pressed("UnlockMM"):
if pressed_esc == 0:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE) # Unlock the mouse
pressed_esc = 1
elif pressed_esc == 1:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) # Lock the mouse
pressed_esc = 0
# Handle mouse motion only when the mouse is locked
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
# Get mouse delta and apply sensitivity
var mouse_delta = event.relative * mouse_sensitivity
# Adjust yaw (horizontal rotation)
yaw -= mouse_delta.x
rotation_degrees.y = yaw
# Adjust pitch (vertical rotation), clamped to avoid flipping
pitch = clamp(pitch - mouse_delta.y, -89, 89)
rotation_degrees.x = pitch