Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Aleph14 · Jul 06, 2015 at 12:42 PM · javascripttouchscreencomputer2 player

Pong 2 players : control paddles by touch screen for PC [.js]

Hi everybody,

Actually, I develop a 2 players Pong for touch screen PC (which have multitouch / window 8) and I work in Javascript. Players are face to face and deplace theirs paddles on y axis.

alt text

My problem is : how control those 2 paddles separately by touch input ?

I successfully control Player1 paddle by using mouse + Raycast but I don't know to do for Player 2 (left click "doesn't work" with touch screen).

there is the code used for Player 1

 private var ray : Ray;
 private var rayCastHit : RaycastHit;
  
 //------------------------------------
 function Update () 
 {
     if(Input.GetMouseButton(0))
     {
         ray = Camera.main.ScreenPointToRay (Input.mousePosition);
  
         if (Physics.Raycast (ray, rayCastHit))
         {
             transform.position.y = rayCastHit.point.y;
         }
     }
 }

I have checked many forums and I find one script which use layer + raycast + touch count but that doesn't work...

     function Update ()
     {
         if(Input.touchCount > 0)
         {
             var TheTouch : Touch = Input.GetTouch (0);
             var ray = Camera.main.ScreenPointToRay (TheTouch.position);
             var hit : RaycastHit;
            
             if (Physics.Raycast (ray,hit,100))
             {
                 if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Paddle01"))
                 {
                 }
               
                else if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Paddle02"))
                {
                }
           }
        }
     }    


Thanks for answer or any advice !

forumimage.jpg (31.7 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by DoTA_KAMIKADzE · Jul 06, 2015 at 12:43 PM

Firstly - you work with touches, not mouse, so use proper classes and functions...

Secondly - check my answer THERE and/or THIS Unity's tutorial, to understand how to start working with multitouch input.

And finally, after you've checked out those links above^ - just detect touches, if on left side of screen then it is for left paddle, if on right then it is for right paddle, and then just move paddles relative to change of touch y position (aka Screen Height).

P.S. And the multiple touches on one side is pretty much up to you, you can either multiply speed or ignore all the other touches while the first one is still pressed or use the last one instead or...

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Move my character where i touched 2 Answers

Issue with some GUITextures and an Array [SOLVED] 1 Answer

How to make a true virtual keyboard for computer in Unity? 0 Answers

GUI textures - hide other guitextures/ objects 0 Answers

object touch 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