Add strafing to the vehicle instead of rotating

This commit is contained in:
reo 2025-03-11 02:35:05 +03:00
parent 1fa06672fb
commit ddbd16db5c

View file

@ -1,8 +1,8 @@
extends CharacterBody3D
@export var acceleration: float = 10.0
@export var brake_intensity: float = 5.0
@export var rotation_speed: float = 1.5
@export var acceleration: float = 5.0
@export var brake_intensity: float = 3.0
@export var strafe_speed: float = 10.0 # Speed for sideways movement
@export var gravity: float = 9.8
@export var max_speed: float = 30.0
@ -15,11 +15,7 @@ func _physics_process(delta: float) -> void:
# Get input direction
var input_forward = Input.get_action_strength("move_forward") - Input.get_action_strength("move_backward")
var input_rotation = Input.get_action_strength("turn_right") - Input.get_action_strength("turn_left")
# Handle rotation
if input_rotation != 0:
rotate_y(-input_rotation * rotation_speed * delta)
var input_strafe = Input.get_action_strength("turn_left") - Input.get_action_strength("turn_right")
# Handle acceleration and braking
if input_forward > 0:
@ -32,9 +28,29 @@ func _physics_process(delta: float) -> void:
# No input, gradually slow down
current_speed = move_toward(current_speed, 0, brake_intensity * delta)
# Apply movement in the direction the vehicle is facing
var direction = -global_transform.basis.z
velocity.z = direction.x * current_speed
velocity.x = direction.z * current_speed
# Get the forward and right vectors
var forward_direction = -global_transform.basis.z
var side_direction = global_transform.basis.x
# Calculate forward movement
var forward_movement = Vector3(
forward_direction.x * current_speed,
0,
forward_direction.z * current_speed
)
# Calculate strafe movement (sideways)
var strafe_movement = Vector3(
side_direction.x * input_strafe * strafe_speed,
0,
side_direction.z * input_strafe * strafe_speed
)
# Combine movements
var total_movement = forward_movement + strafe_movement
# Apply to velocity
velocity.z = total_movement.x
velocity.x = total_movement.z
move_and_slide()