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 :slight_smile:
they don’t move separately.

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

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 :smiley:

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

		
	}
}