- Home /
Pong 2 players : control paddles by touch screen for PC [.js]
Hi everybody,
Actually, I develop a 2 players Pong for touch screen PC (which have multitouch / window 8) and I work in Javascript. Players are face to face and deplace theirs paddles on y axis.
My problem is : how control those 2 paddles separately by touch input ?
I successfully control Player1 paddle by using mouse + Raycast but I don't know to do for Player 2 (left click "doesn't work" with touch screen).
there is the code used for Player 1
private var ray : Ray;
private var rayCastHit : RaycastHit;
//------------------------------------
function Update ()
{
if(Input.GetMouseButton(0))
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, rayCastHit))
{
transform.position.y = rayCastHit.point.y;
}
}
}
I have checked many forums and I find one script which use layer + raycast + touch count but that doesn't work...
function Update ()
{
if(Input.touchCount > 0)
{
var TheTouch : Touch = Input.GetTouch (0);
var ray = Camera.main.ScreenPointToRay (TheTouch.position);
var hit : RaycastHit;
if (Physics.Raycast (ray,hit,100))
{
if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Paddle01"))
{
}
else if (hit.transform.gameObject.layer == LayerMask.NameToLayer("Paddle02"))
{
}
}
}
}
Thanks for answer or any advice !
Answer by DoTA_KAMIKADzE · Jul 06, 2015 at 12:43 PM
Firstly - you work with touches, not mouse, so use proper classes and functions...
Secondly - check my answer THERE and/or THIS Unity's tutorial, to understand how to start working with multitouch input.
And finally, after you've checked out those links above^ - just detect touches, if on left side of screen then it is for left paddle, if on right then it is for right paddle, and then just move paddles relative to change of touch y position (aka Screen Height).
P.S. And the multiple touches on one side is pretty much up to you, you can either multiply speed or ignore all the other touches while the first one is still pressed or use the last one instead or...
Your answer
Follow this Question
Related Questions
Move my character where i touched 2 Answers
Issue with some GUITextures and an Array [SOLVED] 1 Answer
How to make a true virtual keyboard for computer in Unity? 0 Answers
GUI textures - hide other guitextures/ objects 0 Answers
object touch 1 Answer