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 canochaba · Nov 10, 2012 at 02:22 PM · inputiphonetouchtouchcount

Problem with the touch control

Hello,

The game I'm developing is a 2D shoot em up and I'm making a script to control a spaceship but it gives me some errors!

The scripts works like that: When you touch the left side of the screen (button A) the spaceship goes up, and when you touch the right side (button B) the spaceship goes down. If you don't touch the screen, the spaceship just advance normally.

Here's the situation. I hold the A button and the ship goes up, then I hold the B button without releasing A button and the ship goes down. When I release A button the ship still continues going down because I'm already holding B button. Then (and the problem goes now) when I hold A button again the ship don't go up.

I think that the problem comes from the amount of times that the screen gets touched (TouchCount).

Here is a little sketch to make it more clear. Red button is for showing that the button is hold! alt text

I will be very glad if someone wants to give me some advice.

Thanks!

using UnityEngine; using System.Collections;

 public class ShipControl: MonoBehaviour {
 
     public float Speed = 8.0f; 
     private Transform MyTransform;
 
     private enum Position
     {
         Up, Down, Center
     } ;
 
     private Position ShipPosition = Position.Center;
     private float ValueY;
     private Touch Touch1;
     private int tapCount;
     private int i;
     // Start para inicializar. 
     void Start ()
     {
         MyTransform = transform;
     }
 
     void Update () {
 
         if(ShipPosition == Position.Center)
         {
 
             MyTransform.Translate(new Vector2(Speed, 0) * Time.deltaTime);
         }
         else
         {
             MyTransform.Translate(new Vector2(Speed, ValueY) * Time.deltaTime);
         }
 
         ControlFly();   
     }
 
 
     void ControlFly()
     {   
 
         if(Input.touchCount > 0)
         {
 
             for(i = 0; i < Input.touchCount; i++)
             {
                 Touch1 = Input.GetTouch(i);
             }
 
 
             if((Touch1.phase == TouchPhase.Ended || Touch1.phase == TouchPhase.Canceled))
             {
                 iTween.RotateTo(gameObject, iTween.Hash("z", 0, "time", 0.3f, "easetype", "easeinoutsine"));
                 ShipPosition = Position.Center; 
 
             }
 
             if(Touch1.position.x < Screen.width/2)
             {               
                 if((Touch1.phase == TouchPhase.Stationary) || (Touch1.phase == TouchPhase.Moved))
                 {
                     ShipPosition = Position.Up;
                     iTween.RotateTo(gameObject, iTween.Hash("z", 15, "time", 0.3f, "easetype", "easeinoutsine"));
                     ValueY = 10;
                 }
             }
             else
             {
                 if(Touch1.position.x >Screen.width/2)
                 {
                     if((Touch1.phase == TouchPhase.Stationary) || (Touch1.phase == TouchPhase.Moved))
                     {
                         ShipPosition = Position.Down;
                         iTween.RotateTo(gameObject, iTween.Hash("z", -15, "time", 0.3f, "easetype", "easeinoutsine"));
                         ValueY = -10;
                     }
                 }
             }
         }
     }
 
 }


Comment
Add comment · Show 2
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 Kmulla · Nov 10, 2012 at 04:49 PM 0
Share

Just a question: If you change your for-function to this, does it work the opposite then? (Only goes up, when both buttons are pressed)

for (i = Input.touchCount; i > 0; i--) { Touch1 = Input.GetTouch(i); }

avatar image canochaba · Nov 10, 2012 at 06:50 PM 0
Share

Not work, I change your code for “for (i = Input.touchCount; i > 0; i--) { Touch1 = Input.GetTouch(i - 1); }” in order to take the first touch, but now not work the step 3 of the graphic.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by canochaba · Nov 12, 2012 at 02:48 AM

Thanks a lot for the answer, but the problem is that it don't recognize the last touch when both buttons are pressed, i have changed your code in order to get the last touch, but the problem persist like in the picture.

 if(touchingLeft && touchingRight)
             {
                 for(int i = 0; i < Input.touchCount; i++)
                 {
                      MyTouch = Input.GetTouch(i);
                 }
                 
                 if(MyTouch.position.x < Screen.width / 2)
                 {
                     ShipPosition = Position.Up;
                     iTween.RotateTo(gameObject, iTween.Hash("z", 15, "time", 0.3f, "easetype", "easeinoutsine"));
                     ValueY = 10;
                 }
                 else if(MyTouch.position.x > Screen.width /2)
                 {
                     ShipPosition = Position.Down;
                     iTween.RotateTo(gameObject, iTween.Hash("z", -15, "time", 0.3f, "easetype", "easeinoutsine"));
                     ValueY = -10;
                 }
             }
Comment
Add comment · Show 4 · 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 JoshOClock · Nov 12, 2012 at 05:06 AM 0
Share

Sorry I misread your issue.

I think your issue is that the last element in the Touch array won't be the last touch that happened. It's just an array meant to represent the fingers touching the screen. When you touch your first finger that's index[0], When you press down your second finger that becomes index [1] ... even when you release your first finger (index [0]) the second finger is still index [1]. This is on purpose so you can keep track of it.

You'd need to keep track of a LastTouchIndex variable that's in the global scope for your script that you'd set when the phase goes to "began".

Does that make sense?

avatar image canochaba · Nov 12, 2012 at 01:20 PM 0
Share

Thanks, Yes, that's the problem, althought when i release the first finger and press again, the first finger is out.

i.e when the users touch two finger:

touches[0] = finger 1, touches[1] = finger 2

user removes finger 1

touches[0] = null, touches[1] = finger 2

player adds finger

touches[0] = null, touches[1] = finger 2, touches[2] = finger 3

Should I find a way to control when finger is Ended / Canceled for clear the array?

avatar image Demigiant · Nov 12, 2012 at 04:02 PM 0
Share

Uhm, from these results, it seems that the last touch is the highest index. So GetTouch(input.touchCount - 1) should always return the latest one to use?

avatar image canochaba · Nov 12, 2012 at 04:17 PM 0
Share

That will give the same result that my loop "for" is giving me. I've founded a possible solution in this post: http://forum.unity3d.com/threads/144536-How-to-handle-Unity-re-assigning-touches so, I'm going to try and share the results with you.

avatar image
0

Answer by Demigiant · Nov 12, 2012 at 10:01 AM

Hey canochaba :)

Did you try to debug this, and see exactly what's happening?

 // Variable used to avoid showing a log in each update
 int touchCount;
 
 void ControlFly()
 {
   if (touchCount != Input.touchCount) {
     touchCount = Input.touchCount;
     // Does the correct amount of touches show?
     // If not, we can stop here since it's a Unity bug
     Debug.Log("Tot touches: " + touchCount);
   }
   if (touchCount == 0) return;
   // If there are touches,
   // try getting the one you want to use
   // using your preferred left to right precedence
   float screenHalfW = Screen.width * 0.5f;
   Touch activeTouch;
   for(int i = 0; i < touchCount; i++) {
     Touch touch = Input.GetTouch(i);
     if (touch.phase != TouchPhase.Stationary || touch.phase != TouchPhase.Moved) continue;
     if (touch.position.x < screenHalfW || activeTouch == null) activeTouch = touch;
   }
   if (activeTouch != null) {
     // You got an active touch, with left touches
     // taking precedence over right touches
     // YOUR CODE
   } else {
     // Touch ended or touch cancelled
     // YOUR CODE
   }
 }

I wrote this directly here, so I hope I made no typos :P Also, I never used Touch inputs before, but I was wondering why you aren't taking into account the TouchPhase.Began?

Comment
Add comment · Show 2 · 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 canochaba · Nov 12, 2012 at 02:15 PM 0
Share

Thanks $$anonymous$$e, but i still have the same problem. I not take into account the TouchPhase.Began because the behaviour will be the same, I need to keep the last touch index or clear the track. In the previous answer I describe exactly what happens with fingers but i'm obfuscated with this issue :(

avatar image spinki12 · Mar 12, 2013 at 02:26 PM 0
Share

so how you managed this in the end? im having exactly the same problem. working on it nearly two weeks and still can't find solution. searched for milions pages etc but still nothing.any help?

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

13 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

Related Questions

Input.GetTouch SOMETIMES not registering on iOS 0 Answers

GUI texture touch to input 1 Answer

Working with Touch screens... 1 Answer

Joystick / Button 3 Answers

iPhone - How to calculate a decent touch swipe speed? 2 Answers


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