Godot Version
4.7.1.stable
Question
I read in another thread that when attempting to change the size of the player hitbox, especially if you plan on doing it many times, to just make a hitbox for each size and toggle them as needed. I’ve been using the code below in an attempt to make the player crouch with a keybind but every time I press the key my entire computer is bricked. Any suggestions? Is this a bug or is there something to be careful of when setting this kind of thing up? Any help would be appreciated, thanks!
The code in question:
gd
extends CharacterBody3D
@export var speed = 6
@export var mass = 37
# earth's gravity is ~9.8m*s^-2
@export var gravity = 36
# jump_strength measured in meters per second
@export var jump_strength = 12
# friction should be (0 < f <= 1), normal is ~0.007-0.0085
var friction = 0.015
@onready var animation_player = $blockbench_export/AnimationPlayer
var target_velocity = Vector3.ZERO
var gm = gravity * mass
var active_friction = clamp(friction * gm / 100, 0.001, 1)
#runs on startup
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
$InGameMenu.hide()
#runs every frame
func _process(_delta):
if Input.is_action_just_pressed("open_or_close_menu"):
if $InGameMenu.visible:
$InGameMenu.hide()
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
else:
$InGameMenu.show()
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
func _physics_process(delta) -> void:
var input_dir = Input.get_vector("walk_left", "walk_right", "walk_forward", "walk_back")
var direction = transform.basis * Vector3(input_dir.x, 0, input_dir.y)
if Input.is_action_just_pressed("jump") and is_on_floor():
target_velocity.y = jump_strength
animation_player.play("jump")
if Input.is_action_pressed("shift"):
pass
#direction += transform.basis * Vector3(input_dir.x * -6, 0, 0)
# ctrl is the 'run' button in this game
if Input.is_action_pressed("ctrl"):
direction.x *= 2.2
direction.z *= 2.2
active_friction *= 1.05
if Input.is_action_just_pressed("shift"):
$StandingCollision3D.set_deferred("disabled", true)
$SlideCollision3D.set_deferred("disabled", false)
#print("Shift pressed")
if Input.is_action_just_released("shift"):
$StandingCollision3D.set_deferred("disabled", false)
$SlideCollision3D.set_deferred("disabled", true)
#print("Shift released")
if direction != Vector3.ZERO:
direction = direction.normalized()
$Pivot.basis = Basis.looking_at(direction)
target_velocity.x = lerp(velocity.x, direction.x * speed, active_friction)
target_velocity.z = lerp(velocity.z, direction.z * speed ,active_friction)
else:
target_velocity.x = lerp(velocity.x, 0.0, active_friction)
target_velocity.z = lerp(velocity.z, 0.0, active_friction)
if abs(velocity.x) > 0.5 or abs(velocity.z) > 0.5:
animation_player.queue("walk")
else:
animation_player.stop()
if not is_on_floor():
target_velocity.y = target_velocity.y - (gravity * delta)
velocity = target_velocity
move_and_slide()
var sensitivity = 0.002
var pitch = 0.0
func _unhandled_input(event):
if event is InputEventMouseMotion:
# Rotate Yaw (Left/Right) - Rotate the whole character/body
rotate_y(-event.relative.x * sensitivity)
# Rotate Pitch (Up/Down) - Rotate the camera node itself
pitch -= event.relative.y * sensitivity
pitch = clamp(pitch, -PI/2, PI/2)
$Camera3D.rotation.x = pitch
Edit: I’ve pasted the whole script as requested. For context, this is the script running every frame for the player’s avatar, there is very little else going on. Though the problem remains the same.