extends KinematicBody # export variable # const const GRAVITY = -24.8 const JUMP_SPEED = 18 const MOVE_SPEED = 20 const SPRINT_SPEED = 40 const ACCEL = 4.5 const MAX_SLOPE_ANGLE = 40 const MOUSE_SENSITIVITY = 0.05 # private members var _body var _camera var _dir = Vector3(); var _vel = Vector3(); var _is_sprinting; func _ready(): _body = $Body _camera = $Body/Camera Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) func _physics_process(delta): process_input(delta) process_movement(delta) func process_input(delta): # Walking var input_movement_vector = Vector2() if Input.is_action_pressed("move_fwrd"): input_movement_vector.y += 1 if Input.is_action_pressed("move_back"): input_movement_vector.y -= 1 if Input.is_action_pressed("move_left"): input_movement_vector.x -= 1 if Input.is_action_pressed("move_right"): input_movement_vector.x += 1 # look around with mouse _dir = Vector3() var camera_transform = _camera.get_global_transform() _dir += -camera_transform.basis.z * input_movement_vector.y _dir += camera_transform.basis.x * input_movement_vector.x # jumping # TODO: remove after collision is working if Input.is_action_just_pressed("move_jump"): # and is_on_floor(): _vel.y = JUMP_SPEED # sprinting _is_sprinting = Input.is_action_pressed("move_sprint") func process_movement(delta): _vel.y += delta * GRAVITY # set movement speed var target = _dir if _is_sprinting: target *= SPRINT_SPEED else: target *= MOVE_SPEED var hvel = _vel hvel = hvel.linear_interpolate(target, ACCEL * delta) _vel.x = hvel.x _vel.z = hvel.z _vel = move_and_slide(_vel, Vector3(0, 1, 0), 0.05, 4, deg2rad(MAX_SLOPE_ANGLE)) func _input(event): # capture mouse movement if event is InputEventMouseMotion: _body.rotate_x(deg2rad(event.relative.y * MOUSE_SENSITIVITY * -1)) self.rotate_y(deg2rad(event.relative.x * MOUSE_SENSITIVITY * -1)) var camera_rot = _body.rotation_degrees camera_rot.x = clamp(camera_rot.x, -70, 70) _body.rotation_degrees = camera_rot