Here is some Godot GDScript code for an FPS game that allows inspecting the surrounding 3D environment and shows tooltips:
gdscript
extends KinematicBody
export var mouse_sensitivity = 0.1
export var walk_speed = 10
export var gravity = 20
onready var camera = $Camera
func _input(event):
if event is InputEventMouseMotion:
rotate_y(-event.relative.x * mouse_sensitivity)
camera.rotate_x(-event.relative.y * mouse_sensitivity)
camera.rotation.x = clamp(camera.rotation.x, -1.2, 1.2)
func _physics_process(delta):
var move_direction = Vector3()
if Input.is_action_pressed("move_forward"):
move_direction -= camera.global_transform.basis.z
if Input.is_action_pressed("move_back"):
move_direction += camera.global_transform.basis.z
if Input.is_action_pressed("strafe_left"):
move_direction -= camera.global_transform.basis.x
if Input.is_action_pressed("strafe_right"):
move_direction += camera.global_transform.basis.x
move_direction = move_direction.normalized()
move_and_slide(move_direction * walk_speed, Vector3.UP)
var state = get_world().direct_space_state
if Input.is_action_just_pressed("inspect"):
var from = camera.project_ray_origin(get_viewport().size / 2)
var to = from + camera.project_ray_normal(get_viewport().size / 2) * 10
var intersection = state.intersect_ray(from, to)
if intersection:
var tooltip = intersection.collider.get_node("Tooltip")
if tooltip:
tooltip.visible = true
func _unhandled_input(event):
if event.is_action_released("inspect") && tooltip:
tooltip.visible = false
It uses raycasting from the camera to detect objects under the crosshair. When inspecting, it checks if the object has a Tooltip node, and shows it if so. This allows attaching tooltips to interactive objects in the scene.