-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathCharacter_controller_godot.gd
108 lines (78 loc) · 3.51 KB
/
Character_controller_godot.gd
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
extends KinematicBody
export var max_speed = 12
export var acceleration = 60
export var friction = 50
export var air_firction = 10
export var jump_impulse = 20
export var gravity = -40
export var mouse_sensitvity = .1
export var controller_sensitivity = 3
export (int, 0, 10) var push = 1
var velocity = Vector3.ZERO
var snap_vector = Vector3.ZERO
onready var head = $Head
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _unhandled_input(event):
if event.is_action_pressed("click"):
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
if event.is_action_pressed("toggle_mouse_mode"):
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
rotate_y(deg2rad(-event.relative.x * mouse_sensitvity))
head.rotate_x(deg2rad(-event.relative.y * mouse_sensitvity))
func _physics_process(delta):
var input_vector = get_input_vector()
var direction = get_direction(input_vector)
apply_movement(direction, delta)
apply_friction(direction, delta)
apply_gravity(delta)
jump()
apply_controller_rotation()
head.rotation.x = clamp(head.rotation.x, deg2rad(-75), deg2rad(75))
velocity = move_and_slide_with_snap(velocity, snap_vector, Vector3.UP, true, 4, .785398, false)
for idx in get_slide_count():
var collision = get_slide_collision(idx)
if collision.collider.is_in_group("bodies"):
collision.collider.apply_central_impulse(-collision.normal * velocity.length() * push)
func get_input_vector():
var input_vector = Vector3.ZERO
input_vector.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
input_vector.z = Input.get_action_strength("move_back") - Input.get_action_strength("move_forward")
return input_vector.normalized() if input_vector.length() > 1 else input_vector
func get_direction(input_vector):
var direction = Vector3.ZERO
direction = (input_vector.x * transform.basis.x) + (input_vector.z * transform.basis.z)
return direction
func apply_movement(direction, delta):
if direction != Vector3.ZERO:
velocity.x = velocity.move_toward(direction * max_speed, acceleration * delta).x
velocity.z = velocity.move_toward(direction * max_speed, acceleration * delta).z
func apply_friction(direction, delta):
if direction == Vector3.ZERO:
if is_on_floor():
velocity = velocity.move_toward(Vector3.ZERO, friction * delta)
else:
velocity.x = velocity.move_toward(Vector3.ZERO, air_firction * delta).x
velocity.z = velocity.move_toward(Vector3.ZERO, air_firction * delta).z
func apply_gravity(delta):
velocity.y += gravity * delta
velocity.y = clamp(velocity.y, gravity, jump_impulse)
func update_snap_vector():
snap_vector = -get_floor_normal() if is_on_floor() else Vector3.DOWN
func jump():
if Input.is_action_just_pressed("jump") and is_on_floor():
snap_vector = Vector3.ZERO
velocity.y = jump_impulse
if Input.is_action_just_released("jump") and velocity.y > jump_impulse / 2:
velocity.y = jump_impulse / 2
func apply_controller_rotation():
var axis_vector = Vector2.ZERO
axis_vector.x = Input.get_action_strength("look_right") - Input.get_action_strength("look_left")
axis_vector.y = Input.get_action_strength("look_down") - Input.get_action_strength("look_up")
if InputEventJoypadMotion:
rotate_y(deg2rad(-axis_vector.x) * controller_sensitivity)
head.rotate_x(deg2rad(-axis_vector.y) * controller_sensitivity)