【发布时间】:2018-10-09 15:39:15
【问题描述】:
我有一个简单的 3D 篮球游戏,我正在使用具有分屏多人模式的 Unity(v 2017.3.1f1 64 位)...基本上,如果你在屏幕的任一侧向上滑动,那一侧就会射一个球,我检查以确保无论你用多少手指滑动,每边只有一个球被射出,使用 Unity 触摸输入类中的手指 ID……一切似乎都工作正常,包括同时射击在两侧,但是它会随机停止在一侧工作,然后停止接受该侧的输入......有时它会在几秒钟后恢复,有时它只是在游戏的其余部分不起作用......我可以'似乎并没有一致地复制它,但它会经常发生......当我同时在每一侧使用多个手指时,它似乎也更频繁地发生,但是在每一侧使用一个手指时发生了,或者有时当只在一侧滑动......我确信代码可以写得更好,但我们希望让它保持简单尽可能明确...我在这里遗漏了什么吗?...是否有可能使 Touch 数组过载或类似的东西?..构建目标是 PC,并且在 Unity 编辑器和完整构建中都可以看到问题,在多个不同的触摸屏上,包括红外和电容屏...提前感谢您的帮助...这里是相关代码:
int screenMidPoint
int finId1 = -1;
int finId2 = -1;
void Start()
{
Input.multiTouchEnabled = true;
screenMidPoint = Screen.width / 2;
}
void Update()
{
if (Input.touchCount > 0)
{
foreach (var touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
//For left half screen
if (touch.position.x <= screenMidPoint && finId1 == -1)
{
p1StartPos = touch.position;
p1StartTime = Time.time;
finId1 = touch.fingerId;
}
//For right half screen
else if (touch.position.x > screenMidPoint && finId2 == -1)
{
p2StartPos = touch.position;
p2StartTime = Time.time;
finId2 = touch.fingerId;
}
}
else if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
{
if (touch.fingerId == finId1 && Time.time > p1NextFire)
{
p1NextFire = Time.time + fireRate;
p1EndTime = Time.time;
p1EndPos = touch.position;
p1DeltaSwipe = p1EndPos - p1StartPos;
if (p1DeltaSwipe.y > 0)
{
// p1 Shoot code is here
}
finId1 = -1;
}
else if (touch.fingerId == finId2 && Time.time > p2NextFire)
{
p2NextFire = Time.time + fireRate;
p2EndTime = Time.time;
p2EndPos = touch.position;
p2DeltaSwipe = p2EndPos - p2StartPos;
if (p2DeltaSwipe.y > 0)
{
// p2 Shoot code is here
}
finId2 = -1;
}
}
}
}
}
【问题讨论】:
-
finId2 = -1 是否需要在 if 语句之外作为在第一个 else 语句中执行的最后一件事。否则,如果 fingerId 混淆,它可能永远不会重置。抱歉,只是提出了一个想法。
标签: c# unity3d multiplayer split-screen