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
1
Question by KingNeo Gaming · Aug 11, 2014 at 09:23 AM · c#touchmultitouchseperate

MultiTouch for control scheme of my game (C#)

Hey Guys.

I'm fairly new to Unity as well as C#, and I have game mostly developed, but I can't figure out one thing. My game is for mobile devices (IE Galaxy S5, iPhone 5s) and I have three buttons on screen that control the movement of my character. One that makes it jump, one that when held, makes it move right, and another that when held, makes it move left. That all works fine, but there is one issue.

If the player is currently holding down either the 'move left' or 'move right' buttons, then they cannot tap on the jump button in order to jump. If they stop holding the movement buttons and THEN tap the jump button, it works fine.

I have read a few threads on Multitouch, but they all assume/associate with using a single script. Each of my buttons uses a different script in order to keep things simplified and spread out.

The question I have here is: how could I incorporate Multitouch through about 3 different scripts, and allow players to hold down one finger AND press another, but only on the buttons? For reference, here is an image of my game:

alt text

As you see, there are the three buttons. The user can move left and right while holding down their associated buttons with ease, but when holding them down, they cannot press the jump button in order to hop up. Remember, the controls are spread out across 3 separate scripts.

capture.png (28.0 kB)
Comment
Add comment · Show 1
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 Josh Naylor ♦♦ · Aug 11, 2014 at 09:28 AM 0
Share

I'd advise you have all game-play controls in one script like most examples.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Raimi · Aug 11, 2014 at 04:45 PM

I think using one script would be more efficient. I ripped this script from an old project I made, so it wont fit straight into yours.

It checks to see if a Rect I've placed over GUI buttons contains a touch. This is just to give you an idea, its not a complete solution.

         foreach(Touch t in Input.touches)
         {
             Vector2 vec = t.position;
             vec.y = Screen.height - vec.y; // You need to invert since GUI and screen have differnet coordinate system
             
             if(testgame1.buttonLeftRect.Contains(vec))
             {
                 weaponContainer.gameObject.transform.Rotate(0,0,speed);
                 Debug.Log("touched");
             }
             
             if(testgame1.buttonRightRect.Contains(vec))
             {
                 weaponContainer.gameObject.transform.Rotate(0,0,-speed);
             }
             
             if(!fireActive)
             {
                 if(testgame1.buttonFireRect.Contains(vec))
                 {
                     //fireActive = true;
                     
                     if(t.phase == TouchPhase.Began)
                     {
                         kick.animation.Play("test anim");
                         GameObject clone = GameObject.Instantiate(bullet,spawnPoint.transform.position,spawnPoint.transform.rotation) as GameObject;    
                     }
                     if(t.phase == TouchPhase.Ended )
                     {
                         //fireActive = false;
                     }
                 }
             }
         }

I hope this can be useful to you :)


EDIT: More info.

You could try something like this...

 int currentTouch;
 
     //----if there is no touch...
     if(Input.touches.Length <= 0)
     {
         //Do something
     }
     //if there is touch...
     else 
     {
         for(int i = 0; i < Input.touchCount; i++)//loop through touches on screen
         {
             currentTouch = i;
 
             //Check touch phase...
             if(Input.GetTouch(currentTouch).phase == TouchPhase.Stationary)
             {
                 //Get current touch position...
                 var ct = Input.GetTouch(currentTouch).position;
                 ct.y = Screen.height - ct.y;
                 
                 if(UI.boostControl.Contains(ct))
                 {
                     GameObject.Find("Player").SendMessage("Boost");
                 }
             }
         }
     }


Comment
Add comment · Show 6 · 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 KingNeo Gaming · Aug 11, 2014 at 05:51 PM 0
Share

So Im guessing I need to use a 'foreach' loop, as well as use an array to store clicks? Is that the only way I can do it? Also, I know 1 script would be more efficient, but if i can do it over 3, that would be more preferable as it is already set up that way.

avatar image Raimi · Aug 13, 2014 at 01:21 PM 0
Share

Added a suggestion :)

avatar image KingNeo Gaming · Aug 14, 2014 at 03:42 AM 0
Share

Alright I can see that one working, will test tomorrow. Do you know if that will allow for me to do two touches at the same time? IE hold down the button for moving right, and then also tap while still holding the button?

avatar image KingNeo Gaming · Aug 14, 2014 at 04:11 AM 0
Share

Im struggling with this one because each of my buttons is a Sprite with a Box Collider 2D that just moves with the camera (that part was really simple) and that all works fine. I am not able to use the .Contains function with a Unity GameObject such as a Box Collider in order to detect the press. I am not willing to do this another way than how I have it set up with the Sprites and Box Colliders, as I have made it work and have really come to like the system.

avatar image RayJr · Apr 20, 2015 at 11:49 AM 0
Share

Did you ever figure this out?

Show more comments
avatar image
0

Answer by RayJr · Aug 21, 2015 at 11:46 AM

Yup, you capture multiple touches with a for loop. here is one for your moving and jumping thing. notice it jumps on begin only and moves on stationary. THats so he'll only jump once each time you press but continue to move as long as its pressed..

 if (Input.touchCount >0)
 for (int i = 0; i < Input.touchCount; ++i)
 {
     RaycastHit2D hit = Physics2D.Raycast(GUICam.ScreenToWorldPoint(Input.touches[i].position), Vector2.zero);
     if (Input.touches[i].phase == TouchPhase.Began)
     {
 
         switch (hit.collider.name){
         case "JumpButton":
              // jump
             break;
 
         default:
             break;
         }
     }
 
     if (Input.touches[i].phase == TouchPhase.Stationary)
     {
         switch (hit.collider.name){
         
         case "GoLeft":
             //move left
             break;
         case "goRight":
             //go right
             break;
         default:
             break;
         }
     }
 }
 
Comment
Add comment · 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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem with multi touch[solved] 0 Answers

Android 2D multitouch joystick + buttons 0 Answers

MultiTouch Help(C#) 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 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