代码拉取完成,页面将自动刷新
extends Camera2D
@export var zoomSpeed : float = 10;
var zoomTarget :Vector2
var dragStartMousePos = Vector2.ZERO
var dragStartCameraPos = Vector2.ZERO
var isDragging : bool = false
# Called when the node enters the scene tree for the first time.
func _ready():
zoomTarget = zoom
pass # Replace with function body.
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
Zoom(delta)
SimplePan(delta)
ClickAndDrag()
func Zoom(delta):
if Input.is_action_just_pressed("camera_zoom_in"):
zoomTarget *= 1.1
if Input.is_action_just_pressed("camera_zoom_out"):
zoomTarget *= 0.9
zoom = zoom.slerp(zoomTarget, zoomSpeed * delta)
func SimplePan(delta):
var moveAmount = Vector2.ZERO
if Input.is_action_pressed("camera_move_right"):
moveAmount.x += 1
if Input.is_action_pressed("camera_move_left"):
moveAmount.x -= 1
if Input.is_action_pressed("camera_move_up"):
moveAmount.y -= 1
if Input.is_action_pressed("camera_move_down"):
moveAmount.y += 1
moveAmount = moveAmount.normalized()
position += moveAmount * delta * 1000 * (1/zoom.x)
func ClickAndDrag():
if !isDragging and Input.is_action_just_pressed("camera_pan"):
dragStartMousePos = get_viewport().get_mouse_position()
dragStartCameraPos = position
isDragging = true
if isDragging and Input.is_action_just_released("camera_pan"):
isDragging = false
if isDragging:
var moveVector = get_viewport().get_mouse_position() - dragStartMousePos
position = dragStartCameraPos - moveVector * 1/zoom.x
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。