Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 taiomi · Nov 10, 2015 at 01:36 AM · 2dmultiplayertouchtouchscreendragging

Split screen in half - Touch Controls

Hi all,

I was wondering if anyone can help what I am trying to do is split the screen in half so that 2 players can play my pong inspired game using touch controls.

I can only seem to make one paddle move at the moment.

alt text

Here is the code I have on both players-

 var object : GameObject;
 var speed : float = 1;
 var distance : float = 5;
 var touch = Input.GetTouch(0);
 
 
 function Start () {
 
 Input.multiTouchEnabled = true;
 
 }
 
 function Update () {
  
  
  {
 
       
        for (var touch : Touch in Input.touches)     
 
 
      
         {
              if (Input.GetTouch(i).phase == TouchPhase.Began)
      
      
      if (touch.position.y < Screen.height/2)
  {
        
        
         
  {
       var x = Input.touches[0].deltaPosition.x *speed* Time.deltaTime;
       var y = Input.touches[0].deltaPosition.y *speed* Time.deltaTime;
  
       transform.Translate( new Vector3(x, 0 ) );
  
        
              if (transform.position.x > 2)
         {
             transform.position = new Vector3(2, transform.position.y, transform.position.z);
         }
         
         if (transform.position.x < -8)
         {
             transform.position = new Vector3(-8, transform.position.y, transform.position.z);
         }
         }      
        
  }
  
  }
  
  }
  
  }
screen-shot-2015-11-10-at-013021.png (178.0 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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eno-Khaon · Nov 10, 2015 at 06:05 AM

A simple solution to this would be to define each half of your screen with a Rect. As an example, Take your "touch.position" and determine whether it is contained within your Rect.

Here's a basic idea of how I would approach this:

 var p1Paddle: Transform; // Upper half of the screen
 var p1Zone: Rect;
 var p2Paddle: Transform; // Lower half of the screen
 var p2Zone: Rect;
 
 // ...
 
 function Start()
 {
     // See notes after script
     p1Zone = Rect(0, Screen.height * 0.5, Screen.width, Screen.height * 0.5);
     p2Zone = Rect(0, 0, Screen.width, Screen.height * 0.5);
     // ...
 }
 
 function Update()
 {
     // ...
     
     for (var touch : Touch in Input.touches)
     {
         // ...
         
         if(p1Zone.Contains(touch.position))
         {
             // What you have above,
             // but replace transform.position
             // (and similar which use transform data)
             // with p1Paddle.position (and the like)
         }
         else// if(p2Zone.Contains(touch.position)) // implied
         {
             // What you have above again,
             // but replace transform.position
             // (and similar which use transform data)
             // with p2Paddle.position (and the like)
         }
     }
 }

It might seem unusual in the Start() function that it looks like I defined the Rect values opposite what they should be. In fact, this is accounting for differences in the rendering style of OpenGL (bottom to top, left to right) vs. unity GUI position (top to bottom, left to right).

The key element is to separate out the controls to two distinct fields, which can then be freely defined with the pair of Rect values.

Comment
Add comment · Show 1 · 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 taiomi · Nov 10, 2015 at 03:57 PM 0
Share

Thank you so much I will give this a go!

avatar image
0

Answer by shervinn · Mar 13, 2019 at 09:13 PM

Hi, hope you are well.

would this work if i have 4 different games with their own logic and i want to split the screen into 4 but each working independently ?

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 shervinn · Mar 13, 2019 at 10:31 AM

@Eno-Khaon

Hi, hope you are well.

would this work if i have 4 different games with their own logic and i want to split the screen into 4 but each working independently ?

Comment
Add comment · Show 1 · 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 Eno-Khaon · Mar 20, 2019 at 07:26 PM 0
Share

It would.

For any usage of an input position (whether Input.mousePosition or a Touch position), you can divide up your position tests however you want to.

Using Rect as an example:

 // C# example
 // Assu$$anonymous$$g a different camera per sub-game, assign them here
 public Camera[] gameCameras;
 Rect[] gameViews;

 void Start()
 {
     int viewCount = gameCameras.Length;
     gameViews = new Rect[viewCount];
     for(int i = 0; i < viewCount; i++)
     {
         gameViews[i] = gameCameras[i].rect;
     }
 }
 
 void Update()
 {
     for(int i = 0; i < gameViews.Length; i++)
     {
         // The normalized (0-1) position in the individual game window of any size
         Vector2 viewPos = gameCameras[i].ScreenToViewportPoint(Input.mousePosition);
         // Check whether the cursor is within a sub-game before using it
         if(gameViews[i].Contains(viewPos))
         {
             // Do something here
         }
     }
 }

This is untested, but should at least be one of many potential ways to get started.

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

55 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Two finger dragging/rotation of a sprite? 0 Answers

Mobile touch dragging from mouse dragging 1 Answer

Dragging UI Image by touch 3 Answers

Move 2d game object instantly on position of Touch Input. 1 Answer

2 fingers sprite rotation 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