-
Notifications
You must be signed in to change notification settings - Fork 1
/
hero.gd
59 lines (44 loc) · 1.17 KB
/
hero.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
extends KinematicBody
var gravity = -9.8
var velocity = Vector3()
var camera
const SPEED = 3
const ACCEL = 3
const DE_ACCEL = 20
func _ready():
camera = get_node("../Camera").get_global_transform()
func _physics_process(delta):
var dir = Vector3()
var is_moving = false
if Input.is_action_pressed("ui_up"):
dir += -camera.basis[2]
is_moving = true
elif Input.is_action_pressed("ui_down"):
dir += camera.basis[2]
is_moving = true
elif Input.is_action_pressed("ui_left"):
dir += -camera.basis[0]
is_moving = true
elif Input.is_action_pressed("ui_right"):
dir += camera.basis[0]
is_moving = true
dir.y = 0
dir = dir.normalized()
velocity.y += delta * gravity
var hv = velocity
hv.y = 0
var new_pos = dir * SPEED
var accel = DE_ACCEL
if dir.dot(hv) > 0:
accel = ACCEL
hv = hv.linear_interpolate(new_pos, accel * delta)
velocity.x = hv.x
velocity.z = hv.z
velocity = move_and_slide(velocity, Vector3(0,1,0))
if is_moving:
var angle = atan2(hv.x, hv.z)
var rot = self.get_rotation()
rot.y = angle
self.set_rotation(rot)
var speed = hv.length() / SPEED
$HeroMesh/AnimationTreePlayer.blend2_node_set_amount("Idle_Run", speed)