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 jakelong00 · Apr 24, 2014 at 02:49 PM · movementinputtouchmultitouch

MultiTouch issues

I am having 4 textures as player controls in my scene on which I am trying to detect touches using the following code:

 //left,right,attack,jump are textures;

 for (int i = 0; i< Input.touches.GetLength(0); i++) {
                     if (left.HitTest (Input.GetTouch (i).position) && Input.GetTouch (i).phase != TouchPhase.Ended) {
                             //player.SendMessage ("Move", -1);
                     }
                     if (right.HitTest (Input.GetTouch (i).position) && Input.GetTouch (i).phase != TouchPhase.Ended) {
                             //player.SendMessage ("Move", 1);
                     }
                     if (!right.HitTest (Input.GetTouch (i).position) && !left.HitTest (Input.GetTouch (i).position)) {
                             //player.SendMessage ("Move", 0.0f);
                             
                     }
                     if (jump.HitTest (Input.GetTouch (i).position) && Input.GetTouch (i).phase == TouchPhase.Began) {
                             //player.SendMessage ("Jump", true);
                     }
                     if (attack.HitTest (Input.GetTouch (i).position)) {
                             //player.SendMessage ("AttackKeyPressed", true);
                     }
             }

The problem I am getting is that I am unable to detect simultaneous touches on 2 textures like left and jump texture. touching the jump textures stops the left texture action. How can I do this?

Or is there any better strategy to implement this?

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 socialspiel · Apr 24, 2014 at 03:55 PM 0
Share
 if (!right.HitTest (Input.GetTouch (i).position) && !left.HitTest (Input.GetTouch (i).position)) {
                    //player.Send$$anonymous$$essage ("$$anonymous$$ove", 0.0f);
  
               }

What's the purpose for this?

$$anonymous$$ul$$anonymous$$ch doesn't seem to be your problem. Does your movement model work with key presses?

avatar image jakelong00 · Apr 24, 2014 at 05:11 PM 0
Share

of course movement won't work with the lines commented out. I was trying something else, that is why they are commented.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by koray1396 · Apr 24, 2014 at 03:38 PM

SendMessage is not a good way to handle this, instead you may want to access player's script. Untested but something like below.

Also I have added a condition where both left & right buttons are pressed, in your script, it would go right.

 public GameObject player;
 PlayerScript someVariableName;

 void Start(){
     someVariableName = player.GetComponent<PlayerScript>();
 }
 
 void Update(){
 if(Input.touchCount > 0){
 for (int i = 0; i< Input.touchCount; i++) {
     if(right.HitTest (Input.GetTouch(i).position) && left.HitTest (Input.GetTouch (i).position)){
         someVariableName.Move(0.0f);
     } else if (left.HitTest (Input.GetTouch(i).position) && Input.GetTouch (i).phase != TouchPhase.Ended) {
         someVariableName.Move(-1.0f);
     } else if (right.HitTest (Input.GetTouch(i).position) && Input.GetTouch (i).phase != TouchPhase.Ended) {
         someVariableName.Move(1.0f);
     } else if (!right.HitTest (Input.GetTouch(i).position) && !left.HitTest (Input.GetTouch (i).position)) {
         someVariableName.Move(0.0f);
     }
     if (jump.HitTest (Input.GetTouch(i).position) && Input.GetTouch (i).phase == TouchPhase.Began) {
         someVariableName.Jump(true);
     }
     if (attack.HitTest (Input.GetTouch(i).position)) {
         someVariableName.AttackKeyPressed(true);           
     }
 }
 }
 }
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 jakelong00 · Apr 24, 2014 at 05:09 PM 0
Share

I've been meaning to ask this, but can you explain why Send$$anonymous$$essage is not a good way to handle this?? And i think you are missing a for loop in the above code

avatar image koray1396 · Apr 24, 2014 at 05:54 PM 0
Share

yes careless, fixed it now.

I'm not a Unity expert, maybe someone else can give you a much better explanation. But Send$$anonymous$$essage is a longer process, you search for methods by strings, which is unreliable and error prone. Also there were some posts about the performance of this vs GetComponent etc..., proving that Send$$anonymous$$essage is way much slower on tests(about hundreds time slower).

Although there are times that Send$$anonymous$$essage is the only reasonable option, but in your situation, it's not really necessary.

You can also put these directly into your player's script, and test if a GUITexture is touched, another script is not really necessary. You just need to define GUITexture objects.

Hope this helps.

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

Mobile - button press while moving (First Person Controls) 0 Answers

How do I handle multitouch as Input.GetButtonDown()? 0 Answers

¿Unity messing up fingerIDs? [Android] 1 Answer

Mobile game Player follow finger input problem (C#) 2 Answers

How to make responsive Touch inputs? 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