4.0.3
I’m working on a first person 3D game and it’s all been working fine until I tried to make the player and all of it’s children a separate scene. After I saved the branch as a scene, the camera would no longer follow the mouse. I used some advice from a different forum to change the _unhandled_input(event) into just _input(event) and the camera was able to work. but since I saved the branch the UI no longer works and the pause buttons stopped working too. Any help would be greatly appreciated, this ones a real hair puller for me… Here’s the code:
extends CharacterBody3D
var speed
const WALK_SPEED = 5.0
const SPRINT_SPEED = 8.0
const JUMP_VELOCITY = 4.5
const SENSITIVITY = 0.003
var sheathed = true
#bob vars
const BOB_FREQ = 2.0
const BOB_AMP = 0.08
var t_bob = 0.0
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = 14
@onready var head = $Head
@onready var camera = $Head/Camera3D
@onready var hitbox = $Head/Camera3D/hitbox
@onready var hitcol = $Head/Camera3D/hitbox/CollisionShape3D
@onready var animator = $Head/Camera3D/sword/AnimationPlayer
@onready var world = $".."
@onready var pause_menu = $"Pause menu"
#sounds
@onready var grunt = $grunt
@onready var swing =$swing
@onready var unsheath =$unsheath
@onready var ting = $ting
var paused = false
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
pause_menu.hide()
func _input(event):
if event is InputEventMouseMotion:
head.rotate_y(-event.relative.x * SENSITIVITY)
camera.rotate_x(-event.relative.y * SENSITIVITY)
camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-60), deg_to_rad(70))
func _physics_process(delta):
if not paused:
if Input.is_action_pressed("quit"):
get_tree().quit()
if Input.is_action_just_pressed("sheath"):
if sheathed == true:
animator.play("unsheath")
unsheath.play()
sheathed = false
else:
animator.play("sheath")
sheathed = true
if Input.is_action_just_pressed("attack"):
if sheathed == true:
animator.play("unsheath")
unsheath.play()
sheathed = false
else:
animator.play("swing")
swing.play()
await get_tree().create_timer(.4).timeout
hitcol.disabled = false
hitbox.monitoring = true
await get_tree().create_timer(.1).timeout
hitcol.disabled = true
if Input.is_action_just_pressed("interact"):
grunt.play()
if Input.is_action_just_pressed("pause"):
pauseMenu()
if not is_on_floor():
velocity.y -= gravity * delta
# Handle Jump.
if Input.is_action_just_pressed("jump") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Handle Sprint.
if Input.is_action_pressed("sprint") and is_on_floor():
speed = SPRINT_SPEED
else:
speed = WALK_SPEED
# Get the input direction and handle the movement/deceleration.
var input_dir = Input.get_vector("left", "right", "forward", "back")
var direction = (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if is_on_floor():
if direction:
velocity.x = direction.x * speed
velocity.z = direction.z * speed
else:
velocity.x = lerp(velocity.x, direction.x * speed, delta * 7.0)
velocity.z = lerp(velocity.z, direction.z * speed, delta * 7.0)
else:
velocity.x = lerp(velocity.x, direction.x * speed, delta * 3.0)
velocity.z = lerp(velocity.z, direction.z * speed, delta * 3.0)
# Head bob
t_bob += delta * velocity.length() * float(is_on_floor())
var head_bob_offset = _headbob(t_bob)
camera.transform.origin = head.transform.origin + head_bob_offset
move_and_slide()
func _headbob(time) -> Vector3:
var pos = Vector3.ZERO
pos.y = sin(time * BOB_FREQ) * BOB_AMP
pos.x = sin(time * BOB_FREQ/2) * BOB_AMP
return pos
func pauseMenu():
if paused:
print("not paused anymore")
pause_menu.hide()
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
Engine.time_scale = 1
else:
print("paused")
pause_menu.show()
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
Engine.time_scale = 0
paused = !paused
func _on_animation_player_animation_finished(anim_name):
if anim_name == "swing":
hitbox.monitoring = false
func _on_hitbox_area_entered(area):
ting.play()
if area.is_in_group("enemy"):
print("enemy hit")