Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Sweex · Apr 13, 2014 at 02:52 PM · c#touchswipe

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
                 {
                     
                 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Apr 13, 2014 at 05:10 PM 0
Share

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.

avatar image Sweex · Apr 14, 2014 at 10:34 PM 0
Share

Yes, both players will play at the same time. Can you please describe Touc.fingerId bit better?

avatar image ivomarel · Apr 14, 2014 at 11:06 PM 0
Share

https://docs.unity3d.com/Documentation/ScriptReference/Touch-fingerId.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Sweex · Apr 15, 2014 at 11:21 AM 0
Share

I'm gonna dive into your code. Thank you! This photo will explain better what I want to acheieve:

alt text

bez naslova.png (14.0 kB)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges