Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody3D
- #movement variables
- @export var speed := 9
- @export var sprint_speed := 12
- @export var acceleration := 10
- @export var air_acceleration := 4
- var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
- @export var fall_acceleration := 4
- @export var jump_force := 12
- #camera variables
- @onready var camera := $Camera3D
- @onready var move_speed_label := $Camera3D/PlayerUi/GameInfo/PlayerSpeed/MoveSpeed
- @onready var fps_counter := $Camera3D/PlayerUi/GameInfo/FpsCounter
- @export var camera_sensitivity := 0.25
- @export var camera_minimum_clamp := -90
- @export var camera_maximum_clamp := 90
- var velocity_value := Vector3.ZERO
- var collision_info
- #capture mouse
- func _ready() -> void:
- Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
- #mouse inputs
- func _input(event: InputEvent) -> void:
- if event is InputEventMouseMotion:
- rotate_y(event.relative.x * camera_sensitivity * -0.01)
- camera.rotate_x(event.relative.y * camera_sensitivity * -0.01)
- camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(camera_minimum_clamp), deg_to_rad(camera_maximum_clamp))
- #ui stats
- func _process(_delta: float) -> void:
- velocity_value.y = 0
- move_speed_label.text = "Speed : " + "%.2f" % (velocity_value.length())
- fps_counter.text = "Fps : " + str(Engine.get_frames_per_second())
- #player mopvement
- func _physics_process(delta: float) -> void:
- var input_direction := Vector3.ZERO
- #get inputs
- input_direction.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
- input_direction.z = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward")
- var direction = (transform.basis * Vector3(input_direction.x, 0, input_direction.z)).normalized()
- if Input.is_action_just_pressed("jump") and is_on_floor():
- velocity.y = jump_force
- if direction != Vector3.ZERO:
- $Character.look_at(position + direction, Vector3.UP)
- #player movement velocity
- if Input.is_action_pressed("sprint"):
- velocity.x = lerp(velocity.x, direction.x * sprint_speed, acceleration * delta)
- velocity.z = lerp(velocity.z, direction.z * sprint_speed, acceleration * delta)
- elif not is_on_floor():
- velocity.x = lerp(velocity.x, direction.x * speed, air_acceleration * delta)
- velocity.z = lerp(velocity.z, direction.z * speed, air_acceleration * delta)
- else:
- velocity.x = lerp(velocity.x, direction.x * speed, acceleration * delta)
- velocity.z = lerp(velocity.z, direction.z * speed, acceleration * delta)
- #falling velocity
- if not is_on_floor():
- velocity.y = lerp(velocity.y, velocity.y - (gravity), fall_acceleration * delta)
- velocity_value = velocity
- move_and_slide()
Advertisement
Add Comment
Please, Sign In to add comment