gdscript Camera2D 缩放问题(初学者帮助)

问题描述 投票:0回答:1

我正在制作 2D 平台游戏,但我的 Camera2D 遇到问题。我已经有了将相机定位到玩家位置的代码。现在我正在尝试实现一个系统,如果玩家移动(例如按任何移动键 A、D、空格),该系统会增加相机变焦,这是迄今为止我的代码:

extends Camera2D

@onready var player = $"../player"
@onready var target_zoom: Vector2 = Vector2(0.0, 0.0)

const X_LERP_SPEED: float = 0.1
const Y_LERP_SPEED: float = 0.1

const ZOOM_LERP_SPEED: float = 0.1


func _process(delta):
    if Input.is_action_pressed("move_left") or Input.is_action_pressed("move_left") or Input.is_action_pressed("jump"): # if the player moves
        if target_zoom.x <= 0.1: # trying to make zoom smaller if until it reaches 0.1
            target_zoom.x -= 0.01
            target_zoom.y -= 0.01
    else:
        target_zoom = lerp(target_zoom, Vector2(1.0, 1.0), ZOOM_LERP_SPEED * delta) # trying to increase the zoom until it reaches 1 (which is normal size)

    zoom = lerp(zoom, target_zoom * delta, ZOOM_LERP_SPEED) # trying to lerp the Camera2D's zoom property to target_zoom

    # smoothly follows the player (currently the only working thing)
    position.x = lerp(position.x, (player.global_position.x + position.x) / 2, X_LERP_SPEED)
    position.y = lerp(position.y, (player.global_position.y + position.y) / 2, Y_LERP_SPEED)

注意:相机不是玩家节点的子节点。

如你所见,我是一个初学者,并且在 lerping 值、放置

* delta
等方面遇到问题。

我想要实现的目标:相机在不移动时保持常规尺寸 (1),在移动时缩小,在玩家不移动时放大回 1。

camera 2d godot gdscript godot4
1个回答
0
投票

您似乎走在正确的轨道上,但正如您所指出的,您对增量的使用导致事物以奇怪和意想不到的方式扩展。你的逻辑也有一些错误,所以我认为向你展示我处理这个问题的方法是最简单的:

extends Camera2D

@onready var player = $"../player"

# Define the maximum and minimum amount that the camera can zoom
const min_zoom: Vector2 = Vector2(0.1, 0.1)
const max_zoom: Vector2 = Vector2.ONE

# Used to keep track of how zoomed the camera currently is. 
# Note: Ideally, this is assigned using inverse_lerp(min_zoom, max_zoom, zoom), 
#   but unfortunately Godot currently doesn't support Vector2 inverse_lerp.
# Note: Due to note above, if you want to use a different starting zoom, 
#   this value needs to be updated to match.
@onready var zoom_progress: float = 1.0

const ZOOM_OUT_SPEED: float = 0.5
const ZOOM_IN_SPEED: float = 1

const X_LERP_SPEED: float = 0.1
const Y_LERP_SPEED: float = 0.1

func _process(delta):
    # Change zoom_progress based on whether the player is pressing input or not
    if Input.is_action_pressed("move_left") or Input.is_action_pressed("move_right") or Input.is_action_pressed("jump"): # if the player moves
        zoom_progress = clamp(zoom_progress - ZOOM_OUT_SPEED * delta, 0, 1)
    else:
        zoom_progress = clamp(zoom_progress + ZOOM_IN_SPEED * delta, 0, 1)

    zoom = lerp(min_zoom, max_zoom, zoom_progress)

    # smoothly follows the player (currently the only working thing)
    position.x = lerp(position.x, (player.global_position.x + position.x) / 2, X_LERP_SPEED)
    position.y = lerp(position.y, (player.global_position.y + position.y) / 2, Y_LERP_SPEED)

我没有考虑你的运动逻辑,正如你所说,它目前似乎对你有用。

我所做的最大改变是存储一个

zoom_progress
变量来跟踪相机的变焦程度,并根据输入进行调整。

我还添加了具有不同放大和缩小速度的功能,因为根据我的经验,您希望相机放大的速度比缩小的速度快,但如果不适合,请随意将其更改回单一速度您的需求!

另一个调整是将最小变焦从

Vector2.ZERO
移动到
Vector2(0.1, 0.1)
,因为 Godot 不喜欢相机的变焦为零,如果你尝试的话会抛出警告。

如果您想更好地掌握如何使用 delta,Jonas Tyroller 的这个视频帮助我了解了很多基础知识,当我开始感到困惑时,我经常会回顾它!

© www.soinside.com 2019 - 2024. All rights reserved.