Ball movement

How to move the ball across the screen by touching the screen while the ball moves only in the axis x or y (depending on the direction in which I move my finger). The ball may not move diagonally.

Some idea?

By any chance, are you talking about iPhone ?

Yes, i am talking about iPhone

You can drag an object in the plane of the camera using the technique described in this thread. As for limiting the movement to X or Y only, you can do this by checking the movement delta at the start of the drag. Basically, just see whether the X or Y part of the delta is greater and use only that value for the rest of the drag.

I have a problem when returning values touch.position phase iPhoneTouchPhase.Moved to the point t2, because they do not always return a value. In most cases, done properly, but there are cases when you do not return anything for both or one of the axis values.

Do you have any suggestions?

The code is below.

private var t1 : Vector2;
private var t2 : Vector2;
var speed : float = 10.0;
var canmove : boolean = true;

function Update() {

for(var touch : iPhoneTouch in iPhoneInput.touches) {

var hit : RaycastHit;

var ray = Camera.main.ScreenPointToRay (Input.mousePosition);

if (touch.phase == iPhoneTouchPhase.Began canmove == true) {
t1 = touch.position;
canmove = false;
}

if (touch.phase == iPhoneTouchPhase.Moved canmove == false) {
t2 = touch.position;

}

if (touch.phase == iPhoneTouchPhase.Ended canmove == false) {

if (t2.direction == transform.right) {

transform.position = transform.right*speed;
}

canmove = true;
}

}

}