34 lines
No EOL
1.2 KiB
GDScript
34 lines
No EOL
1.2 KiB
GDScript
extends Spatial
|
|
|
|
onready var camera := $Camera
|
|
|
|
var mouseOrigin: Vector2
|
|
var lastCameraTransform: Transform
|
|
|
|
func _input(event: InputEvent):
|
|
# Camera zoom
|
|
if event.is_action("zoom_in"):
|
|
camera.zoom(true)
|
|
elif event.is_action("zoom_out"):
|
|
camera.zoom(false)
|
|
# Save original camera and mouse position before panning
|
|
if event.is_action_pressed("pan"):
|
|
mouseOrigin = get_viewport().get_mouse_position()
|
|
lastCameraTransform = camera.transform
|
|
|
|
func _process(delta: float):
|
|
# If panning, translate mouse delta to camera delta
|
|
if Input.is_action_pressed("pan"):
|
|
var mouseDelta := get_viewport().get_mouse_position() - mouseOrigin
|
|
var mousePos: Vector2 = mouseDelta * 0.0096 * (1-camera.getZoomLevel()/5) # Magic numbers everywhere
|
|
camera.transform.origin = lastCameraTransform.origin - Vector3(mousePos.x, 0, mousePos.y)
|
|
|
|
func _physics_process(delta: float):
|
|
# Raycast mouse to check for hover
|
|
var mousePos := get_viewport().get_mouse_position()
|
|
var from: Vector3 = camera.project_ray_origin(mousePos)
|
|
var to: Vector3 = from + camera.project_ray_normal(mousePos) * 1000
|
|
var space_state := get_world().direct_space_state
|
|
var result := space_state.intersect_ray(from, to)
|
|
if result.size() > 0:
|
|
print(result) |