- Home /
Swipe on the part of the screen
Hi. I have 2 players in my game. I want to make separated controls for them. If I swipe left or right my player would go left or right. But I want same with my second player. So is there any way I can separate controls, for first player I should swipe on the left part of the screen and for the second, I should swipe on the right part of the screen? I already have swipe controls in my script, I only need to detect on which part of the screen player swipes. Greetings!
foreach(Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fp = touch.position;
lp = touch.position;
}
if (touch.phase == TouchPhase.Moved )
{
lp = touch.position;
}
if(touch.phase == TouchPhase.Ended)
{
if((fp.x - lp.x) > 80) // left swipe
{
}
else if((fp.x - lp.x) < -80) // right swipe
{
}
This will depend on your criteria. What happens if a swipe starts on one side and ends on the other. The simplest would be to define side by starting position, then you could do:
if (fp.x < Screen.width / 2.0f) {
// Left side of the screen
}
else {
// Right side of the screen
}
If both player can play at the same time, then your code will significantly more complicated. You cannot just look at the Input.touches array. You will need to track each finger independently using the Touch.fingerId.
Yes, both players will play at the same time. Can you please describe Touc.fingerId bit better?
Answer by robertbu · Apr 15, 2014 at 12:55 AM
Here is a bit of code that uses fingerID and identifies swipe (or at least finger down and finger up) on each side of the screen. I'm sure there is more work to be done, but this should give you the general idea. Create a GUIText object, attach this script and build to your device:
using System.Collections;
using System.Collections.Generic;
[RequireComponent (typeof (GUIText))]
public class FingerSwipe : MonoBehaviour {
public struct FingerData {
public FingerData(int f, Vector2 p, float t) {
fingerID = f;
startPos = p;
startTime = t;
}
public int fingerID;
public Vector2 startPos;
public float startTime;
}
private List<FingerData> data = new List<FingerData>();
private float mid;
void Start() {
mid = Screen.width / 2.0f;
}
void Update () {
if (Input.touchCount == 0) {
data.Clear();
}
else {
foreach (Touch t in Input.touches) {
if (t.phase == TouchPhase.Began) {
FingerData d = new FingerData(t.fingerId, t.position, Time.time);
data.Add (d);
}
else if (t.phase == TouchPhase.Ended) {
int i = data.FindIndex (x => x.fingerID == t.fingerId);
if (i == -1) {
Debug.Log ("No matching Begin");
}
if (i >= 0) {
FingerData d = data[i];
data.RemoveAt(i);
if (d.startPos.x < mid && t.position.x < mid) {
guiText.text = d.fingerID.ToString () + ": left side swipe";
} else if (d.startPos.x >= mid && t.position.x >= mid) {
guiText.text = d.fingerID.ToString () + ": right side swipe";
} else {
guiText.text = d.fingerID.ToString () + ": overlap swipe";
}
}
}
}
}
}
}
I don't know how your game works, but you may want to establish a certain distance between the finger down and finger up before it becomes a swipe. You may want that distance measured as a fraction of the pixel width of the screen to keep things semi-platform independent. I don't know your game mechanic, but this code currently allows multiple fingers on each side. So it may be possible for players to "cheat" by swiping with multiple fingers.
I'm gonna dive into your code. Thank you! This photo will explain better what I want to acheieve:
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Swipe to move object: object sometimes flying off screen even though value is clamped 1 Answer
Detect swipe up gesture IOS 1 Answer
Swipe and Hold to move character . 1 Answer