SlimeoosOdyssey/godot/Player.gd

51 lines
1.7 KiB
GDScript

extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
@export var horiz_sens = 0.25
@export var vert_sens = 0.25
var playerCam
var playerVisuals
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
func _ready():
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED
func _process(delta):
# Brings your mouse out of the window if you press escape. Add pause screen function here?
if Input.is_action_pressed("ui_cancel"):
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
func _input(event):
# Handle the rotation of the camera using mouse movement.
if event is InputEventMouseMotion:
# Side to side, first player object then player visuals.
rotate_y(deg_to_rad(-event.relative.x * horiz_sens))
$grape_man.rotate_y(deg_to_rad(event.relative.x * horiz_sens))
# Vertical camera rotation.
playerCam.rotate_x(deg_to_rad(-event.relative.y * vert_sens))
$CameraMount.global_rotation.x = clampf($CameraMount.global_rotation.x, deg_to_rad(-60), deg_to_rad(-11))
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
# Get the input direction and handle the movement/deceleration.
# As good practice, you should replace UI actions with custom gameplay actions.
var input_dir = Input.get_vector("ui_left", "ui_right", "ui_up", "ui_down")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
move_and_slide()