# MouseOrbit not working with touch drag

hi folks, I am trying to use the MouseOrbit script, it works fine if i use my mouse (click and drag to orbit) but i got this touchscreen and when i touch and drag on screen it does nothing. its a “single touch” screen, im using the touch to emulate mouse click, it works fine in other places such as clicking a button etc. any idea how to fix this problem?
btw i tried another script by cyb3rmaniak from this thread
and it works with touch drag but it resets the cam position if i retouch the screen.

``````var target : Transform;
var distance = 10.0;

var xSpeed = 250.0;
var ySpeed = 120.0;

var yMinLimit = -20;
var yMaxLimit = 80;

private var x = 0.0;
private var y = 0.0;

function Start () {
var angles = transform.eulerAngles;
x = angles.y;
y = angles.x;

// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
}

function LateUpdate () {

if(Input.GetMouseButton(0)) { //Mouse button is down
//Change the angles by the mouse movement
x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
y = ClampAngle(y, yMinLimit, yMaxLimit);

var rotation = Quaternion.Euler(y, x, 0);
var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;

transform.rotation = rotation;
transform.position = position;

//Zooming with mouse
distance += Input.GetAxis("Mouse ScrollWheel")*distance;
}

}

static function ClampAngle (angle : float, min : float, max : float) {
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp (angle, min, max);
}
``````

I got this idea from another post and wrote the following script, it partially solved my problem, the touch drag works but now i have another problem, if i take my finger off or even stop dragging the mouse (button down) and then on the next touch or click, it doesnt read it as a neutral point but instead the moment you do the next touch or click it starts to orbit relative to the first click/touch that you did the moment simulation started. any suggestions?

``````var target : Transform;

var distance = 10.0;

var xSpeed = 250.0;

var ySpeed = 120.0;

var startX;

var startY;

var yMinLimit = -20;

var yMaxLimit = 80;

private var x = 0.0;

private var y = 0.0;

function Start () {

startX = Input.mousePosition.x;

startY = Input.mousePosition.y;

var angles = transform.eulerAngles;

x = angles.y;

y = angles.x;

// Make the rigid body not change rotation

if (rigidbody)

rigidbody.freezeRotation = true;

}

function LateUpdate () {

if(Input.GetMouseButton(0)) { //Mouse button is down

var currentX = Input.mousePosition.x;

var differenceX = currentX-startX;

var currentY = Input.mousePosition.y;

var differenceY = currentY-startY;

x +=  differenceX*xSpeed*Time.deltaTime*0.01;

y -=  differenceY*ySpeed*Time.deltaTime*0.01;

y = ClampAngle(y, yMinLimit, yMaxLimit);

var rotation = Quaternion.Euler(y, x, 0);

var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;

transform.rotation = rotation;

transform.position = position;

//Zooming with mouse

distance += Input.GetAxis("Mouse ScrollWheel")*distance;

}

}

static function ClampAngle (angle : float, min : float, max : float) {

if (angle < -360)

angle += 360;

if (angle > 360)

angle -= 360;

return Mathf.Clamp (angle, min, max);

}
``````

adding the following code solved the problem for the mouse but not with the touch. for touch i have to touch twice in the new places to initiate orbiting from that point… weird

`````` else if (Input.GetMouseButtonUp) {

startX = Input.mousePosition.x;

startY = Input.mousePosition.y;

}
``````

p.s. why is that copy pasting code add extra blank line in between two lines.