Unity双玩家移动端控制问题:如何实现单区域仅计一次触控
Hey there! Let's sort out this control logic for your split-screen two-player setup. The key problem right now is that you're counting every individual touch in a region, which is why multiple fingers on one side are multiplying your rotation speed. Instead, we just need to check if any touch is active in each region—regardless of how many fingers are there.
Here's a straightforward solution:
Step 1: Define Your Screen Regions
First, decide how to split the screen (most common is left/right for two players, but you can adjust this for top/bottom or custom areas). For this example, we'll split it down the middle.
Step 2: Track Touch Presence Per Region
Instead of counting touches, use boolean flags to mark if a region has at least one active touch. This way, even if 4 fingers are on one side, the flag only stays true—no stacking.
Step 3: Update Your Control Logic
Loop through all active touches, update the region flags, then use those flags to drive your rotation (or other actions) once per region.
Here's the code example tailored to your use case:
void Update() { // Initialize flags to false each frame bool leftRegionTouched = false; bool rightRegionTouched = false; // Check all current touches for (int i = 0; i < Input.touchCount; i++) { Touch currentTouch = Input.GetTouch(i); // Only consider active touches (ignore ended/canceled ones) if (currentTouch.phase == TouchPhase.Began || currentTouch.phase == TouchPhase.Moved || currentTouch.phase == TouchPhase.Stationary) { // Check which region the touch is in if (currentTouch.position.x < Screen.width / 2) { leftRegionTouched = true; } else { rightRegionTouched = true; } } } // Apply rotation logic based on region flags if (leftRegionTouched) { // Left player's rotation—this runs once per frame, no matter how many fingers RotateLeftPlayer(); } if (rightRegionTouched) { // Right player's rotation RotateRightPlayer(); } } // Example rotation methods (replace with your existing logic) void RotateLeftPlayer() { // Adjust the rotation axis/speed to match your needs leftPlayerTransform.Rotate(Vector3.up, Time.deltaTime * baseRotationSpeed); } void RotateRightPlayer() { rightPlayerTransform.Rotate(Vector3.up, -Time.deltaTime * baseRotationSpeed); }
Why This Works
- We’re no longer counting the number of touches—we’re just checking if any touch exists in each region. Even if 4 fingers are on the left,
leftRegionTouchedonly gets set totrueonce, so your rotation logic doesn’t multiply. - The touch phase check ensures we don’t accidentally count touches that are ending or canceled, which can cause unexpected behavior.
Extra Tips
- Custom Regions: If you need non-split-screen regions (like circles or custom rectangles), replace the
x < Screen.width/2check with a bounds check. For example, useRect.Contains(currentTouch.position)to check if the touch is inside a predefined rectangle. - Configurable Split: Make the split point a public variable so you can adjust it in the Unity Inspector:
public float screenSplitRatio = 0.5f; // 0.5 = middle // Then use: currentTouch.position.x < Screen.width * screenSplitRatio - Single-Tap Triggers: If you need to detect a single tap (not hold) for an action, use
TouchPhase.Beganin a separate check instead of the combined phase logic.
内容的提问来源于stack exchange,提问作者Kali

