Godot_3D_controller/player.gd

61 lines
2.0 KiB
GDScript

extends CharacterBody3D
const SPEED = 5.0
const JUMP_VELOCITY = 4.5
# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var mouse_origin
@export var sensitivity = 200
func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta
# Handle jump.
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
var direction = Vector3.ZERO
var modifier = 1
# We check for each move input and update the direction accordingly.
if Input.is_action_pressed("move_right"):
rotation.y -= 0.04
if Input.is_action_pressed("move_left"):
rotation.y += 0.04
if Input.is_action_pressed("move_back"):
direction += transform.basis.z
modifier = 0.5
$"Pivot/Root Scene/AnimationPlayer".play("CharacterArmature|Run_Back")
if Input.is_action_pressed("move_forward"):
direction -= transform.basis.z
$"Pivot/Root Scene/AnimationPlayer".play("CharacterArmature|Run")
if direction != Vector3.ZERO && is_on_floor():
direction = direction.normalized()
velocity.x = direction.x * SPEED * modifier
velocity.z = direction.z * SPEED * modifier
elif is_on_floor():
$"Pivot/Root Scene/AnimationPlayer".play("CharacterArmature|Idle")
velocity.x = 0
velocity.z = 0
move_and_slide()
func _input(event):
# Changing mouse to Warp Mode on right mouse
if event is InputEventMouseButton && event.button_index == 2:
if event.pressed == true:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
mouse_origin = event.position
if event.pressed == false:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
# Handling rotation on mouse movement
if Input.is_action_pressed("right_click"):
if event is InputEventMouseMotion:
rotation.y -= event.relative.x / sensitivity
$CameraPivot.rotation.x -= event.relative.y / sensitivity
$CameraPivot.rotation.x = clamp($CameraPivot.rotation.x, deg_to_rad(-45), deg_to_rad(90))