35 lines
919 B
GDScript
35 lines
919 B
GDScript
extends CharacterBody3D
|
|
class_name Player
|
|
|
|
var input_dir
|
|
|
|
@onready var body: MeshInstance3D = $Body
|
|
|
|
|
|
const MOVE_SPEED = 10.0
|
|
const MOVE_TRANSITION_SPEED = 7.0
|
|
|
|
func _physics_process(delta: float) -> void:
|
|
if is_on_floor():
|
|
standard_movement(delta)
|
|
|
|
body_look_at_mouse()
|
|
apply_gravity(delta)
|
|
move_and_slide()
|
|
|
|
func body_look_at_mouse():
|
|
var mouse_raycast = MousePos.get_mouse_world_position()
|
|
if mouse_raycast != null:
|
|
body.look_at(Vector3(mouse_raycast.x,body.global_position.y,mouse_raycast.y),Vector3.UP)
|
|
|
|
func apply_gravity(delta):
|
|
if !is_on_floor():
|
|
velocity.y -= 9.8 * delta
|
|
|
|
func standard_movement(delta):
|
|
input_dir = Input.get_vector("move_left", "move_right", "move_forward", "move_backward")
|
|
|
|
if input_dir != null:
|
|
velocity.x = lerp(velocity.x, input_dir.x * MOVE_SPEED,delta * MOVE_TRANSITION_SPEED)
|
|
velocity.z = lerp(velocity.z, input_dir.y * MOVE_SPEED,delta * MOVE_TRANSITION_SPEED)
|