如何使用新的Unity输入系统用滑动移动相机?

发布于 2025-02-11 20:05:02 字数 822 浏览 0 评论 0原文

使用Unity的新输入系统,我想检测一个开始触摸和结束触摸。使用这些,我想计算相机移动的距离和方向,因此相机以“滑动”移动。但是,我在弄清楚如何在Update()中检测这些触摸时遇到了困难。

伪代码将是:

private void Update() {

// get start position
// get end position
   MoveCameraOnSwipe()
}

我已经弄清楚了MoveCameraOnsWipe()中的情况,或者如何使用目标位置和方向移动相机。我唯一不知道的部分是如何使用新输入系统捕获更新中的开始和结束值。

我可以尝试直接读取值,但不确定它将如何工作:

我们可以使用此方法从触摸中返回值,其中屏幕控件是播放器输入:

public Vector2 PrimaryPosition()
    {
        return screenControls.SwipeMap.PrimaryPosition.ReadValue<Vector2>();
    }

然后在update()中 - 我会做类似的事情

     screenControls.SwipeMap.PrimaryContact.started += context => StartTouchPrimary(context);
     screenControls.SwipeMap.PrimaryContact.canceled += context => EndTouchPrimary(context);

: 't看起来很完整。任何帮助将不胜感激。

Using Unity's new input system, I would like to detect a start touch and and end touch. Using these, I'd like to compute the distance and direction for the camera to move, so the camera moves with a "swipe". However, I am having trouble figuring out how to detect these touches in Update().

Pseudocode would be:

private void Update() {

// get start position
// get end position
   MoveCameraOnSwipe()
}

I've figured out what goes in MoveCameraOnSwipe(), or how to move the camera using a target position and direction. The only part I can't figure out is how to use the new input system to capture the start and end values in Update.

I could try to read the values directly, but not sure how that would work:

We could return values from the touch using this method, where Screen Controls is player input:

public Vector2 PrimaryPosition()
    {
        return screenControls.SwipeMap.PrimaryPosition.ReadValue<Vector2>();
    }

Then in Update() - would I do something like:

     screenControls.SwipeMap.PrimaryContact.started += context => StartTouchPrimary(context);
     screenControls.SwipeMap.PrimaryContact.canceled += context => EndTouchPrimary(context);

But this doesn't seem quite complete. Any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动次打次papapa 2025-02-18 20:05:02

Onable中注册事件,而从它们中取消注册是ondisable。这样可以防止悬挂引用,这可能会导致内存泄漏(因为仍然存在与事件有关的引用)。

上开始将位置存储到变量startposition

screenControls.SwipeMap.PrimaryContact.started += ctx => {
    startPosition = ctx.ReadValue<Vector2>();
}

上取消将值存储到变量endposition

screenControls.SwipeMap.PrimaryContact.canceled += ctx => {
    endPosition = ctx.ReadValue<Vector2>();

    // At this point you want to take a copy of the start and end positions,
    // calculate the distance and direction, and start  the camera move
}

在开始运动之前,请确保副本的起点和终点位置(尤其是起始位置)。 startposition可以在进行相机运动时重新分配(考虑完成刷卡,并立即启动新的滑动)。

要进行更新,请添加布尔值cameramoving,该将指示您的运动功能是否应运行。当取消发生时,应将布尔值设置为true,并在移动完成后设置为false。

private void Update()
{
    if (cameraMoving)
    {
        MoveCameraOnSwipe();
    }
}

如果在相机移动时发生新的滑动,则可以将startposition设置为当前的相机位置,以防止相机跳回新的Swipes启动位置。

Register to the events in OnEnable, and unregister from them is OnDisable. This prevents hanging references, which can cause memory leaks (since there is a reference tied to the event still).

On started store the position to a variable startPosition.

screenControls.SwipeMap.PrimaryContact.started += ctx => {
    startPosition = ctx.ReadValue<Vector2>();
}

On canceled store the value to a variable endPosition.

screenControls.SwipeMap.PrimaryContact.canceled += ctx => {
    endPosition = ctx.ReadValue<Vector2>();

    // At this point you want to take a copy of the start and end positions,
    // calculate the distance and direction, and start  the camera move
}

Make sure to take a copy of the start and end positions (notably the start position) before starting the movement. startPosition can be reassigned while the camera movement is taking place (think completing a swipe, and immediately starting a new swipe).

For Update, add a boolean cameraMoving which will indicate if your movement function should be running. The boolean should be set to true when cancel happens and set to false when the movement is complete.

private void Update()
{
    if (cameraMoving)
    {
        MoveCameraOnSwipe();
    }
}

If a new swipe happens while the camera is moving, you can set startPosition to the current camera position to prevent the camera jumping back to the new swipes starting position.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文