Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody3D
- const SPEED = 5.0
- const JUMP_VELOCITY = 12
- @export_group("Camera")
- @export_range(0.0, 1.0) var mouse_sensitivity := 0.25
- @export_group("Movement")
- @export var move_speed := 8.0
- @export var acceleration := 20.0
- @export var rotation_speed := 12.0
- @onready var _camera_pivot: Node3D = %Camera_Pivot
- @onready var _camera: Camera3D = %Camera3D
- @onready var _skin: MeshInstance3D = %MeshInstance3D
- @onready var _springarm: SpringArm3D = %SpringArm3D
- var _camera_input_direction := Vector2.ZERO
- var _last_movement_direction := Vector3.BACK
- func _input(event: InputEvent) -> void:
- if event.is_action_pressed("left_click"):
- Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
- if event.is_action_pressed("ui_cancel"):
- Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
- #camera zoom
- if event.is_action_pressed("zoom_in"):
- _springarm.spring_length -= 0.2
- if event.is_action_pressed("zoom_out"):
- _springarm.spring_length += 0.2
- func _unhandled_input(event: InputEvent) -> void:
- var is_camera_motion := (
- event is InputEventMouseMotion and
- Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED
- )
- if is_camera_motion:
- _camera_input_direction = event.screen_relative * mouse_sensitivity
- func _physics_process(delta: float) -> void:
- var raw_input := Input.get_vector("left", "right", "back", "forward")
- var forward := -(_camera.global_basis.z)
- var right := _camera.global_basis.x
- var move_direction := forward * raw_input.y + right * raw_input.x
- move_direction.y = 0.0
- move_direction = move_direction.normalized()
- velocity = velocity.move_toward(move_direction * move_speed, acceleration * delta)
- # Add the gravity.
- if not is_on_floor():
- velocity += get_gravity() * delta
- # Handle jump.
- if Input.is_action_just_pressed("ui_accept") and is_on_floor():
- velocity.y = JUMP_VELOCITY
- #camera stuff
- _camera_pivot.rotation.x -= -(_camera_input_direction.y) * delta
- _camera_pivot.rotation.x = clamp(_camera_pivot.rotation.x, -PI / 4.0, PI / 8.0)
- _camera_pivot.rotation.y -= _camera_input_direction.x * delta
- _camera_input_direction = Vector2.ZERO
- _springarm.spring_length = clamp(_springarm.spring_length, 0.5, 6)
- move_and_slide()
- if move_direction.length() > 0.2:
- _last_movement_direction = move_direction
- var target_angle := Vector3.BACK.signed_angle_to(_last_movement_direction, Vector3.UP)
- _skin.global_rotation.y = lerp_angle(_skin.rotation.y, target_angle, rotation_speed * delta)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement