extends KinematicBody
onready var pivot: Position3D = $Pivot
export var air_friction = 2
export var friction = 9
export var speed := 10.0
export var jump_strength := 20.0
export var gravity := 40
export var acel = 70
var _velocity := Vector3.ZERO
var _snap_vector := Vector3.DOWN
var control_sens = 0.8
var move_direction = Vector3.ZERO
func _ready():
print(pivot)
func _physics_process(delta):
move_direction.x = Input.get_action_strength("move_right") - Input.get_action_strength("move_left")
move_direction.z = Input.get_action_strength("move_backward") - Input.get_action_strength("move_forward")
var direction = get_direction(move_direction)
apply_movement(move_direction, direction, delta)
_velocity.y -= gravity * delta
var just_landed = is_on_floor() and _snap_vector == Vector3.ZERO
var is_jumping = is_on_floor() and Input.is_action_just_pressed("jump")
if is_jumping:
_velocity.y = jump_strength
_snap_vector = Vector3.ZERO
elif just_landed:
_snap_vector = Vector3.DOWN
_velocity = move_and_slide_with_snap(_velocity, _snap_vector, Vector3.UP, true)
apply_friction(direction, delta)
func get_direction(move_direction):
var direction = (move_direction.x * CamScript.transform.basis.x) + (move_direction.z * CamScript.transform.basis.z)
return direction
func _process(delta):
pass
func apply_movement(move_direction, direction, delta):
if direction != Vector3.ZERO:
_velocity.x = _velocity.move_toward(direction * speed, acel * delta).x
_velocity.z = _velocity.move_toward(direction * speed, acel * delta).z
pivot.look_at(global_transform.origin + direction, Vector3.UP)
func apply_friction(direction, delta):
if direction == Vector3.ZERO:
if is_on_floor():
_velocity = _velocity.move_toward(Vector3.ZERO, friction * delta)
else:
_velocity.x = _velocity.move_toward(direction * speed, air_friction * delta).x
_velocity.z = _velocity.move_toward(direction * speed, air_friction * delta).z
