如何使用新的Unity输入系统用滑动移动相机?
使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
Onable
中注册事件,而从它们中取消注册是ondisable
。这样可以防止悬挂引用,这可能会导致内存泄漏(因为仍然存在与事件有关的引用)。在
上开始
将位置存储到变量startposition
。在
上取消
将值存储到变量endposition
。在开始运动之前,请确保副本的起点和终点位置(尤其是起始位置)。
startposition
可以在进行相机运动时重新分配(考虑完成刷卡,并立即启动新的滑动)。要进行更新,请添加布尔值
cameramoving
,该将指示您的运动功能是否应运行。当取消
发生时,应将布尔值设置为true,并在移动完成后设置为false。如果在相机移动时发生新的滑动,则可以将
startposition
设置为当前的相机位置,以防止相机跳回新的Swipes启动位置。Register to the events in
OnEnable
, and unregister from them isOnDisable
. 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 variablestartPosition
.On
canceled
store the value to a variableendPosition
.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 whencancel
happens and set to false when the movement is complete.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.