/
cubio.gd
65 lines (51 loc) · 1.98 KB
/
cubio.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
extends CharacterBody3D
const MAX_SPEED = 3.5
const JUMP_SPEED = 6.5
const ACCELERATION = 4
const DECELERATION = 4
@onready var camera = $Target/Camera3D
@onready var gravity = -ProjectSettings.get_setting("physics/3d/default_gravity")
@onready var start_position = position
func _physics_process(delta):
if Input.is_action_just_pressed(&"exit"):
get_tree().quit()
if Input.is_action_just_pressed(&"reset_position") or global_position.y < - 6:
# Pressed the reset key or fell off the ground.
position = start_position
velocity = Vector3.ZERO
var dir = Vector3()
dir.x = Input.get_axis(&"move_left", &"move_right")
dir.z = Input.get_axis(&"move_forward", &"move_back")
# Get the camera's transform basis, but remove the X rotation such
# that the Y axis is up and Z is horizontal.
var cam_basis = camera.global_transform.basis
cam_basis = cam_basis.rotated(cam_basis.x, -cam_basis.get_euler().x)
dir = cam_basis * dir
# Limit the input to a length of 1. length_squared is faster to check.
if dir.length_squared() > 1:
dir /= dir.length()
# Apply gravity.
velocity.y += delta * gravity
# Using only the horizontal velocity, interpolate towards the input.
var hvel = velocity
hvel.y = 0
var target = dir * MAX_SPEED
var acceleration
if dir.dot(hvel) > 0:
acceleration = ACCELERATION
else:
acceleration = DECELERATION
hvel = hvel.lerp(target, acceleration * delta)
# Assign hvel's values back to velocity, and then move.
velocity.x = hvel.x
velocity.z = hvel.z
# TODO: This information should be set to the CharacterBody properties instead of arguments: , Vector3.UP
# TODO: Rename velocity to linear_velocity in the rest of the script.
move_and_slide()
# TODO: This information should be set to the CharacterBody properties instead of arguments.
# Jumping code. is_on_floor() must come after move_and_slide().
if is_on_floor() and Input.is_action_pressed(&"jump"):
velocity.y = JUMP_SPEED
func _on_tcube_body_entered(body):
if body == self:
get_node(^"WinText").show()