- Home /
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);
}
}
}
}
}
}
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.
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 ?
Answer by shervinn · Mar 13, 2019 at 10:31 AM
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 ?
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
Follow this Question
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