- Edited
Hello everyone.
I followed this guide but i don't have achievement all i wish for character movements.
*1. The lack in the code to block the vertical movement of the camera rotation.
*2. The lack of smooth movement during the sudden change of direction.
(Godot 4.1.1.stable)
This is mine solutions; (fixed to the one in tutorial video guide):
1. Camera with vertical fix limit rotation and fixed on player (can be improved)
extends CharacterBody3D
@onready var visuals = $visuals as Node3D
#with inside skeleton and mesh
@onready var mount := $mount as Node3D
#node on "model neck"
@onready var spring := $mount/spring as SpringArm3D
#camera on mouse motion
var mouse_sensitivity := 0.5
var target_angle : float
const ANGULAR_SPPED : float = TAU
func _ready() -> void:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
mount.rotate_y(deg_to_rad( - event.relative.x * mouse_sensitivity))
spring.rotate_x(deg_to_rad( - event.relative.y * mouse_sensitivity))
spring.rotation.x = clampf(spring.rotation.x, -PI/4, PI/4)
spring.rotation.z = clampf(spring.rotation.y, -PI/4, PI/4)
2. Smooth movement during the sudden change of direction
func _physics_process(delta: float) -> void:
var input_dir := Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction := (mount.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if input_dir.length_squared() > 0:
target_angle = 0.75 * TAU - direction.signed_angle_to(Vector3(1,0,0), Vector3(0,1,0))
var angle_diff := wrapf(target_angle - visuals.rotation.y, -PI, PI)
if direction:
visuals.rotation.y += clampf(delta * ANGULAR_SPPED, 0, absf(angle_diff)) * signf(angle_diff)
velocity.x = direction.x * movement_speed
velocity.z = direction.z * movement_speed
else:
velocity.x = 0.0
velocity.z = 0.0
This portion of code, from the tutorial, need to be delete to make it work:
visuals.look_at(position + direction)