summaryrefslogtreecommitdiffstats
path: root/character/fps_controller/fps_controller.gd
diff options
context:
space:
mode:
authormikatomik <mikec_2007@hotmail.com>2022-09-09 23:56:34 -0400
committermikatomik <mikec_2007@hotmail.com>2022-09-09 23:56:34 -0400
commit8199603264cb1e4649e52cb1ae8a5b800104a1c1 (patch)
tree2b0c68cb275f4a97ebf90ffdfcc9623a1f3c4b6a /character/fps_controller/fps_controller.gd
parent941ec1c9ed595ef8b039ddf083e5bc910a8c4ecb (diff)
downloadgame_jam49-8199603264cb1e4649e52cb1ae8a5b800104a1c1.tar.gz
game_jam49-8199603264cb1e4649e52cb1ae8a5b800104a1c1.zip
Implement basic FPS controller
Diffstat (limited to 'character/fps_controller/fps_controller.gd')
-rw-r--r--character/fps_controller/fps_controller.gd58
1 files changed, 58 insertions, 0 deletions
diff --git a/character/fps_controller/fps_controller.gd b/character/fps_controller/fps_controller.gd
new file mode 100644
index 0000000..e5f119b
--- /dev/null
+++ b/character/fps_controller/fps_controller.gd
@@ -0,0 +1,58 @@
+extends KinematicBody
+
+onready var camera = $Camera
+onready var ray = $Camera/RayCast
+
+export var walk_speed : int = 5
+export var accel : float = 0.2
+export var gravity : int = -1
+export var term_velocity : int = -35
+
+var velocity : Vector3
+
+func _ready():
+ Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
+
+func _process(_delta):
+ get_input()
+
+func _physics_process(_delta):
+ handle_movement()
+
+func get_input():
+ var dir : Vector3 = Vector3.ZERO
+
+ #Get base movement input
+
+ if Input.is_action_pressed("forward"):
+ dir += -global_transform.basis.z
+ if Input.is_action_pressed("backward"):
+ dir += global_transform.basis.z
+ if Input.is_action_pressed("left"):
+ dir += -global_transform.basis.x
+ if Input.is_action_pressed("right"):
+ dir += global_transform.basis.x
+
+ dir = dir.normalized()
+
+ #Pull X and Z values from directional input. Velocity.y will be handled in physics loop
+
+ velocity.x = lerp(velocity.x, dir.x * walk_speed, accel)
+ velocity.z = lerp(velocity.z, dir.z * walk_speed, accel)
+
+func handle_movement():
+ #Apply gravity
+ velocity.y += gravity
+ if velocity.y < term_velocity:
+ velocity.y = term_velocity
+
+ velocity = move_and_slide(velocity, Vector3.UP)
+
+func _input(event):
+ #Handle mouse movement for camera
+ if event is InputEventMouseMotion:
+ self.rotate_y(-event.relative.x * Settings.mouse_sens)
+ camera.rotate_x(-event.relative.y * Settings.mouse_sens)
+ camera.rotation.x = clamp(camera.rotation.x, -1.2, 1.2)
+
+