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 Djaydino · Aug 19, 2012 at 07:51 PM · androidmultiplayertouch

2 player touch movement

Hi i am still new @ programming and i am working on a Pong like game for android / iOS. I want the paddle to move for 2 players separately,

i tried like this :

//----------------------------------------

private var ray : Ray; private var rayCastHit : RaycastHit;

function Update()
{ for (var touchP1 : Touch in Input.touches)

if(touchP1.phase == TouchPhase.Moved) { ray = Camera.main.ScreenPointToRay (touchP1.position);

if (Physics.Raycast (ray, rayCastHit)) { transform.position.x = rayCastHit.point.x; } } }

//-------------------------------------------

i made 1 with touchP1 and 1 with touchP2

and attached 1 to each paddle but that did not work well :) they don't move separately.

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
0
Best Answer

Answer by Djaydino · Aug 24, 2012 at 06:31 AM

i found a good script and edited it for my need and this works now and only behind the paddles just the way i wanted it :D

 class PlayerController extends MonoBehaviour
 {    
     var inEditorSpeed : float = 1;    
     private var hasTwoPlayers : boolean = true;
     private var accelAxis : int = 0;
     private var touchAxis : int = 0;
     private var smooth : float;
     private var touchSmooth: float;
     var touchSpeed : float = 0.1;
     private var touchVelocity : float;
     private var touchVelocity2 : float;
     private var player1 : Transform;
     var player2 : Transform;
     var cam : Camera;
     
      
     
     function Start() 
     {            
         cam = gameObject.FindWithTag("MainCamera").GetComponent("Camera");
         player1 = transform;
     }
     
     function Update () {
         
             for (var touch : Touch in Input.touches)
             {
                     var p : Vector3 = cam.ScreenToWorldPoint (Vector3 (touch.position.x,touch.position.y,0));
                       
                     if (p.x < -9.5)
                         player1.position.y = Mathf.SmoothDamp(player1.position.y, p.y, touchVelocity, touchSpeed);
                     else
                     {
                         if (hasTwoPlayers)
                         {
                             if (p.x > 9.5)
                             player2.position.y = Mathf.SmoothDamp(player2.position.y, p.y, touchVelocity2, touchSpeed);
                         }
                     }
             }
 
         
     }
 }
 
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 reptilebeats · Aug 19, 2012 at 08:52 PM

it wouldnt work because all you are doing is using a different name, their still both the same, you want to test where your hit is, so maybe divide the screen in half and say if touch is on this side then do this but if theres touch on the other side then do something else.

the iphone has no way of knowing which finger touched the screen only where the touch is how it behaves and how many are on it, so you could either make a couple colliders and test for a hit on them or check for touch on the screen in a defined position or area of the screen

Comment
Add comment · Show 14 · 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 reptilebeats · Aug 19, 2012 at 08:56 PM 0
Share

ok just had to re$$anonymous$$g myself what pong was, something like this would probably be better to do using bluetooth or online as the iphone screens are a bit small, either way you will want to test for a hit only on one side and make the object follow on one axis

avatar image Djaydino · Aug 19, 2012 at 09:28 PM 0
Share

can it also be a certain part of the screen and how best to do that?

i am planing to make it for tablets and ipad as indeed it will be small to play on an iphone or android phone.

avatar image reptilebeats · Aug 19, 2012 at 09:40 PM 0
Share

me personally i use colliders as triggers just as its a easier to resize them, but for testing for a hit on one side of the screen then i guess you would use the camera size in vector 4 to get the width and height and define what areas can be selected here is a link i found a while back to a pretty straightforward answer

http://answers.unity3d.com/questions/143918/touching-left-or-right-side-of-iphone-screen.html

avatar image reptilebeats · Aug 19, 2012 at 09:41 PM 0
Share

here i just stole their answer

if (Input.touchCount == 1) { var touch = Input.touches[0]; if (touch.position.x < Screen.width/2) { DoLeftSideStuff(); } else if (touch.position.x > Screen.width/2) { DoRightSideStuff(); } }

avatar image reptilebeats · Aug 19, 2012 at 09:45 PM 0
Share

simply put that is getting the screen width and dividing it by two to get half the screen and then testing if their is a touch in that area.

i dont think this will work for two fingures touching both side so for that you will want to put a simple loop function to test for each hit

Show more comments

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

10 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

Related Questions

Unity Vuforia video error 0 Answers

Android Keyboard Issues 0 Answers

input touch android help 0 Answers

any one now about the touch events of android swipe events on the android 1 Answer

Phase end not working on android 0 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