Godot 4.3
When i’m rotating my camera, sometimes it’s turning upside down and then you can’t turn it back, maybe i forgot something?
Here’s my script:
extends CharacterBody3D
const SENSIVITY = 0.005
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
const BOB_FREQ = 2.0
const BOB_AMP = 0.08
var t_bob = 0.0
@onready var neck = $Node3D
@onready var camera = $Node3D/Camera3D
@onready var anim = $Node3D/AuxScene/AnimationPlayer
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _unhandled_input(event: InputEvent) → void:
if event is InputEventMouseMotion:
neck.rotate_y(-event.relative.x * SENSIVITY)
camera.rotate_x(-event.relative.y * SENSIVITY)
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad (-60), deg_to_rad(60))
func _physics_process(delta):
if not is_on_floor():
velocity += get_gravity() * delta
if Input.is_action_just_pressed(“ui_accept”) and is_on_floor():
velocity.y = JUMP_VELOCITY
animations()
var input_dir = Input.get_vector(“a”, “d”, “w”, “s”)
var direction = (neck.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
t_bob += delta * velocity.length() * float(is_on_floor())
camera.transform.origin = headbob(t_bob)
move_and_slide()
func animations():
if velocity.x != 0:
anim.play(“Running0”)
else:
anim.play(“StandingIdle0”)
if not is_on_floor():
anim.play(“Jumping(2)0”)
func headbob(time) → Vector3:
var pos = Vector3.ZERO
pos.y = sin(time * BOB_FREQ) * BOB_AMP
pos.x = cos(time * BOB_FREQ / 2) * BOB_AMP
return pos