Repository navigation
Expand file tree
/
Copy pathdefault_character.gd
More file actions
64 lines (48 loc) · 1.8 KB
/
Copy pathdefault_character.gd
File metadata and controls
64 lines (48 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
@tool
extends CharacterBody3D
@export_category("Default Character")
@export var move_speed: float = 5.0
@export var mouse_sensitivity: float = 0.1
@export var jump_velocity: float = 4.5
var _head: Node3D
var _mouse_input: Vector2 = Vector2.ZERO
var _gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity")
func _ready() -> void:
if get_tree().edited_scene_root == self:
return
_head = $Head
_head.rotation.y = rotation.y
_head.rotation.x = clamp(rotation.x, deg_to_rad(-90), deg_to_rad(90))
rotation = Vector3.ZERO
func _physics_process(delta: float) -> void:
if get_tree().edited_scene_root == self:
return
if not is_on_floor():
velocity.y -= _gravity * delta
if Input.is_action_pressed("pilot_jump") and is_on_floor():
velocity.y = jump_velocity
var input_dir := Vector2.ZERO
if Input.is_action_pressed("pilot_move_forward"):
input_dir.y += 1
if Input.is_action_pressed("pilot_move_back"):
input_dir.y -= 1
if Input.is_action_pressed("pilot_move_left"):
input_dir.x -= 1
if Input.is_action_pressed("pilot_move_right"):
input_dir.x += 1
input_dir = input_dir.normalized()
var forward := -_head.global_transform.basis.z
var right := _head.global_transform.basis.x
var direction := (right * input_dir.x + forward * input_dir.y)
direction.y = 0
direction = direction.normalized()
velocity.x = direction.x * move_speed
velocity.z = direction.z * move_speed
move_and_slide()
_head.rotation_degrees.y -= _mouse_input.x * mouse_sensitivity
_head.rotation_degrees.x -= _mouse_input.y * mouse_sensitivity
_head.rotation_degrees.x = clamp(_head.rotation_degrees.x, -90.0, 90.0)
_mouse_input = Vector2.ZERO
func _unhandled_input(event: InputEvent) -> void:
if event is InputEventMouseMotion and Input.mouse_mode == Input.MOUSE_MODE_CAPTURED:
_mouse_input += event.relative