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.
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.