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.

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);
		}
	    }      
       
 }
 
 }
 
 }
 
 }

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.

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 ?

@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 ?