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 claudiu4444 · Jun 21, 2014 at 04:48 PM · javascripttouchscreenmultitouch

Multi-touch problem in a 2 player pong game

Hello, I'm trying to make a 2 player pong game for android and I have some problems with the multi touch: Lets say I am player 1, and my brother is player 2,if my brother removes his finger from screen, and I keep my finger on screen, his paddle will continue to go up/ or down till I remove my finger. That won't happen if I am the only one using the touchscreen, it only happenes with both of us use it

Here is the code from player one:

 var speed : float = 10;
 
 
 function Update () {
 
     if (Input.touchCount > 0) 
     {
         var touchDeltaPos:Vector2 = Input.GetTouch(0).position;
         if(Input.touchCount>1)
         var touchDeltaPos2:Vector2 = Input.GetTouch(1).position;
         if(touchDeltaPos.x<Screen.width/2)
         {
             if(touchDeltaPos.y > Screen.height/2)
             {
                 rigidbody2D.velocity.y = 1*speed;
             }
             else rigidbody2D.velocity.y = -1*speed;
         }
         else if(touchDeltaPos2.x<Screen.width/2&&Input.touchCount>1)
         {
             if(touchDeltaPos2.y > Screen.height/2)
             {
                 rigidbody2D.velocity.y = 1*speed;
             }
             else rigidbody2D.velocity.y = -1*speed;
         }
     }
     if (Input.touchCount == 0)
         rigidbody2D.velocity.y = 0;
     rigidbody2D.velocity.x=0;
 }

here is the code from player 2:

 var speed : float = 10;
 
 
 function Update () {
 
     if (Input.touchCount > 0) 
     {
         var touchDeltaPos:Vector2 = Input.GetTouch(0).position;
         if(Input.touchCount>1)
         var touchDeltaPos2:Vector2 = Input.GetTouch(1).position;
         if(touchDeltaPos.x>Screen.width/2)
         {
             if(touchDeltaPos.y > Screen.height/2)
             {
                 rigidbody2D.velocity.y = 1*speed;
             }
             else rigidbody2D.velocity.y = -1*speed;
         }
         else if(touchDeltaPos2.x > Screen.width/2&&Input.touchCount>1)
         {
             if(touchDeltaPos2.y > Screen.height/2)
             {
                 rigidbody2D.velocity.y = 1*speed;
             }
             else rigidbody2D.velocity.y = -1*speed;
         }
     }
     if (Input.touchCount == 0)
         rigidbody2D.velocity.y = 0;
     rigidbody2D.velocity.x=0;
 }
 

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by thunderrabbit · Jul 27, 2015 at 04:42 AM

Here's my solution in C#. I use this script for both players, and just check to see if the touch is close to the player's Y axis. If so, I assume the touch must be intended for that user.

 void Update () {
     if (Input.touchCount > 2) {
         return;        // dashing the dreams of four player mode
     }

     foreach (Touch touch in Input.touches)
     {
         Vector3 touchPos =  Camera.main.ScreenToWorldPoint (touch.position);

         Vector2 myPostion = gameObject.GetComponent<Rigidbody2D> ().position;

         // if touch is within 2 units of my vertical axis
         if(Mathf.Abs(touchPos.x - myPostion.x) <= 2) {
             myPostion.y = Mathf.Lerp (myPostion.y, touchPos.y, (speed + speedIncrease));
             myPostion.y = Mathf.Clamp (myPostion.y, -14, 14);    // stay between -14 and 14 (screen height; YMMV)
             gameObject.GetComponent<Rigidbody2D> ().position  = myPostion;
         }
     }
 }
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
avatar image
0

Answer by NoseKills · Jun 21, 2014 at 06:10 PM

In both scripts you are resetting the speed.y only IF touch count is 0. You should do the reset IF there are no touches or if neither of the touches happen on that player's side of the screen

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

Camera.main.WorldToScreenPoint(touch.position). Help !!! 1 Answer

Does Win7 multitouch work in Unity 3.4+ or not? 0 Answers

Multitouch loses touch even when finger still on screen. 0 Answers

how to apply multiple Functionality like touch moves with Buttons move..? 0 Answers

Multitouch iPhone error! Help 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