- Home /
2 player touch movement
Hi i am still new @ programming and i am working on a Pong like game for android / iOS. I want the paddle to move for 2 players separately,
i tried like this :
//----------------------------------------
private var ray : Ray; private var rayCastHit : RaycastHit;
function Update()
{ for (var touchP1 : Touch in Input.touches)
if(touchP1.phase == TouchPhase.Moved) { ray = Camera.main.ScreenPointToRay (touchP1.position);
if (Physics.Raycast (ray, rayCastHit)) { transform.position.x = rayCastHit.point.x; } } }
//-------------------------------------------
i made 1 with touchP1 and 1 with touchP2
and attached 1 to each paddle but that did not work well :) they don't move separately.
Answer by Djaydino · Aug 24, 2012 at 06:31 AM
i found a good script and edited it for my need and this works now and only behind the paddles just the way i wanted it :D
class PlayerController extends MonoBehaviour
{
var inEditorSpeed : float = 1;
private var hasTwoPlayers : boolean = true;
private var accelAxis : int = 0;
private var touchAxis : int = 0;
private var smooth : float;
private var touchSmooth: float;
var touchSpeed : float = 0.1;
private var touchVelocity : float;
private var touchVelocity2 : float;
private var player1 : Transform;
var player2 : Transform;
var cam : Camera;
function Start()
{
cam = gameObject.FindWithTag("MainCamera").GetComponent("Camera");
player1 = transform;
}
function Update () {
for (var touch : Touch in Input.touches)
{
var p : Vector3 = cam.ScreenToWorldPoint (Vector3 (touch.position.x,touch.position.y,0));
if (p.x < -9.5)
player1.position.y = Mathf.SmoothDamp(player1.position.y, p.y, touchVelocity, touchSpeed);
else
{
if (hasTwoPlayers)
{
if (p.x > 9.5)
player2.position.y = Mathf.SmoothDamp(player2.position.y, p.y, touchVelocity2, touchSpeed);
}
}
}
}
}
Answer by reptilebeats · Aug 19, 2012 at 08:52 PM
it wouldnt work because all you are doing is using a different name, their still both the same, you want to test where your hit is, so maybe divide the screen in half and say if touch is on this side then do this but if theres touch on the other side then do something else.
the iphone has no way of knowing which finger touched the screen only where the touch is how it behaves and how many are on it, so you could either make a couple colliders and test for a hit on them or check for touch on the screen in a defined position or area of the screen
ok just had to re$$anonymous$$g myself what pong was, something like this would probably be better to do using bluetooth or online as the iphone screens are a bit small, either way you will want to test for a hit only on one side and make the object follow on one axis
can it also be a certain part of the screen and how best to do that?
i am planing to make it for tablets and ipad as indeed it will be small to play on an iphone or android phone.
me personally i use colliders as triggers just as its a easier to resize them, but for testing for a hit on one side of the screen then i guess you would use the camera size in vector 4 to get the width and height and define what areas can be selected here is a link i found a while back to a pretty straightforward answer
http://answers.unity3d.com/questions/143918/touching-left-or-right-side-of-iphone-screen.html
here i just stole their answer
if (Input.touchCount == 1) { var touch = Input.touches[0]; if (touch.position.x < Screen.width/2) { DoLeftSideStuff(); } else if (touch.position.x > Screen.width/2) { DoRightSideStuff(); } }
simply put that is getting the screen width and dividing it by two to get half the screen and then testing if their is a touch in that area.
i dont think this will work for two fingures touching both side so for that you will want to put a simple loop function to test for each hit
Your answer
![](https://koobas.hobune.stream/wayback/20220613075905im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Unity Vuforia video error 0 Answers
Android Keyboard Issues 0 Answers
input touch android help 0 Answers
any one now about the touch events of android swipe events on the android 1 Answer
Phase end not working on android 0 Answers