First_Person_Slapper/Player.gd

67 lines
1.9 KiB
GDScript3
Raw Normal View History

extends KinematicBody
2022-10-11 09:26:29 -04:00
export var speed = 20
export var h_acceleration = 6
export var gravity = 40
export var jump = 30
export var mouse_sensitivity = 0.03
var full_contact = false
var direction = Vector3()
var h_velocity = Vector3()
var movement = Vector3()
var gravity_vec = Vector3()
onready var head = $Head
2022-10-11 09:26:29 -04:00
onready var ground_check = $GroundCheck
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _input(event):
if event is InputEventMouseMotion:
rotate_y(deg2rad(-event.relative.x * mouse_sensitivity))
head.rotate_x(deg2rad(-event.relative.y * mouse_sensitivity))
head.rotation.x = clamp(head.rotation.x, deg2rad(-89), deg2rad(89))
2022-10-11 09:26:29 -04:00
func _physics_process(delta):
if Input.is_action_just_pressed("uncapture_mouse"):
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
elif Input.is_action_just_released("uncapture_mouse"):
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
direction = Vector3()
full_contact = ground_check.is_colliding()
if not is_on_floor():
gravity_vec += Vector3.DOWN * gravity * delta
elif is_on_floor() and full_contact:
gravity_vec = -get_floor_normal() * gravity
else:
gravity_vec = -get_floor_normal()
if Input.is_action_just_pressed("jump") and (is_on_floor() or ground_check.is_colliding()):
gravity_vec = Vector3.UP * jump
if Input.is_action_pressed("move_forward"):
direction -= transform.basis.z
elif Input.is_action_pressed("move_backward"):
direction += transform.basis.z
if Input.is_action_pressed("move_left"):
direction -= transform.basis.x
elif Input.is_action_pressed("move_right"):
direction += transform.basis.x
direction = direction.normalized()
h_velocity = h_velocity.linear_interpolate(direction * speed, h_acceleration * delta)
movement.z = h_velocity.z + gravity_vec.z
movement.x = h_velocity.x + gravity_vec.x
movement.y = gravity_vec.y
move_and_slide(movement, Vector3.UP)