Swipe To Look(Nearly Complete)

Ok here’s my current swipe to look script, it works ok but doesn’t respond well to quick swipes. Which makes sense being that it stops when you take your finger off the screen, it also will keep going while your finger is still on the screen for a short while, I tried a couple ways to stop this but they were either really inaccurate and slow or were really jittery. Ideally I want to be able to test my characters current facing direction against where its supposed to have turned based on the swipe, and if they are equal to each other and you have not swiped again then it would stop, it seems that would be far more precise, but I don’t know how I would go about doing that as I’m turning based off a vector2 and my character has a vector3 for turning although its locked to y-axis turning (I set the mouselookiphone for x axis only). So how would I go about implementing that, can anyone point me in the right direction? Thanks.

var Look : MouseLookiPhone;
var tPos : Vector2;

function Update () {
SwipeToLook();
}

function SwipeToLook(){
Look.CustomUpdate();
	for (var i=0; i < iPhoneInput.touchCount; i++){
		var touch : iPhoneTouch = iPhoneInput.touches[i];
        if(touch.phase == iPhoneTouchPhase.Moved){
        tPos = touch.deltaPosition;
        }
        if(touch.phase == iPhoneTouchPhase.Ended){
        tPos = Vector2.zero;
        }
    }
    Look.SetInput(tPos);
}

As I understand, if you swipe your finger to the left, you want him to rotate fixed amount of degrees to the left automatically. is it correct?

Precisely, unless the person swipes again then that would cancel the original swipe.

Here is the basic script for rotating objects fixed amount of degrees. its using right and left arrow key for the rotation, but it shouldn’t be difficult to modify it for touch input.

private var SwipeRight;
private var SwipeLeft;
private var RotationControl=0;
private var AngleAxis;

var RotationDegree=0;

function Update()
{
	if(Input.GetKeyDown("right") )
	{	
		SwipeRight=true;
	}
	else if(Input.GetKeyDown("left") )
	{	
		SwipeLeft=true;
	}
	if(Input.GetKeyUp("right"))
	{
		RotationControl+=RotationDegree;
	}
	else if(Input.GetKeyUp("left") )
	{
		RotationControl-=RotationDegree;
	}
	
	if(SwipeRight == true)
	{
		SwipeLeft=false;
		AngleAxis= Quaternion.AngleAxis(RotationControl, Vector3.up);
		transform.rotation=Quaternion.Slerp (transform.rotation, AngleAxis, Time.deltaTime * 3.0);
	}
	if(SwipeLeft == true)
	{
		SwipeRight=false;
		AngleAxis= Quaternion.AngleAxis(-RotationControl, Vector3.up);
		transform.rotation=Quaternion.Slerp (transform.rotation, AngleAxis, Time.deltaTime * 3.0);
	}
	if(RotationControl == 360 || RotationControl == -360)
			RotationControl=0;
}

Ok so I combined the to scripts and its working… kinda. The problem is, is that the degree variable from the swipe is constantly changing based on how much you swipe even if you barely move your finger, is there someway I can smooth that out a little by only having it move if the swipe is greater than a set value? Thanks heres the script.

EDIT: Ok well I got the limiter in but now it moves really slow and is not smooth at all. Reposted new code with limiter below.

var Look : MouseLookiPhone;
var tPos : Vector2;
var done : boolean = true;
var turnSpeed : float = 3.0;
var btPos : float;
var noTurn : float;

function Update () {
for (var i=0; i < iPhoneInput.touchCount; i++){
var touch : iPhoneTouch = iPhoneInput.touches[i];
if(touch.phase == iPhoneTouchPhase.Moved){
done = false;
tPos = touch.deltaPosition;
Debug.Log(tPos.x);
}
}
if(tPos.x == 360 || tPos.x == -360){
tPos.x=0;
}
if(tPos.x - btPos > noTurn){
Turn();
}
btPos = tPos.x;
}

function Turn(){
AngleAxis= Quaternion.AngleAxis(tPos.x, Vector3.up);
transform.rotation=Quaternion.Slerp (transform.rotation, AngleAxis, Time.deltaTime * turnSpeed);
}

You need to capture the first touch when touchPhaze Began, this way it only stores one value, if you use moved it will update,

here, i use a “confort zone”

		if(Input.touchCount > 0)
		        {
			iPhoneTouch touch = iPhoneInput.touches[0];
			
			
			switch (touch.phase) {

			case iPhoneTouchPhase.Began:

			touchStart =  touch.position.x;
							
			break;
				
			case iPhoneTouchPhase.Moved:
                      if(Mathf.Abs(touchStart - touch.position.x)  > zonadeconforto)
				{
					//DO SOMETHING THE FINGER IS DRAGGIN
									
				}
				else
				{
					//ITS JUST A TAP
				}				
			break;
			case iPhoneTouchPhase.Stationary:				
			break;
			case iPhoneTouchPhase.Ended:
			break;
					
			}
		
	
	}

Thanks! I went and worked on the script and this is what I got, it works but now I have to work on making it rotate smooth, and help on that would be greatly appreciated, thanks!

var aPosX;
var bPosX;
var moveMag : float = 0.0;
var moveSpeed : float = 2.0;
var speed : float = 0.9;
var magMaxPos : float = 5;
var magMaxNeg : float = -1;

function Update () {
	for (var i=0; i < iPhoneInput.touchCount; i++){
		var touch : iPhoneTouch = iPhoneInput.touches[i];
		if(touch.phase == iPhoneTouchPhase.Began){
			aPosX = touch.position.x;
            Debug.Log("touched " + aPosX);
		}
		if(touch.phase == iPhoneTouchPhase.Moved){
			bPosX = touch.position.x;
		}
		if(touch.phase == iPhoneTouchPhase.Ended){
			aPosX=bPosX;
		}
		if(aPosX!=bPosX){
			moveMag = (bPosX - aPosX)/moveSpeed;
		}else{
			moveMag = moveMag * speed;
		}
        if(moveMag >= magMaxPos){
        transform.Rotate(0, moveMag, 0);
        moveMag = magMaxPos;
		aPosX=bPosX;
        }
        else if(moveMag <= magMaxNeg){
        transform.Rotate(0, moveMag, 0);
        moveMag = magMaxNeg;
		aPosX=bPosX;
        }
    }
}

Ok I tried to smooth it out using the code which is of now commented out (cause it didn’t work, it jumped all over the place randomly). Here it is, did I do something wrong, is there a better way?

var aPosX;
var bPosX;
var moveMag : float = 0.0;
var moveSpeed : float = 2.0;
var speed : float = 0.9;
var magMaxPos : float = 5;
var magMaxNeg : float = -1;
//var tSpeed : float = 3;

function Update () {
	for (var i=0; i < iPhoneInput.touchCount; i++){
		var touch : iPhoneTouch = iPhoneInput.touches[i];
		if(touch.phase == iPhoneTouchPhase.Began){
			aPosX = touch.position.x;
            Debug.Log("touched " + aPosX);
		}
		if(touch.phase == iPhoneTouchPhase.Moved){
			bPosX = touch.position.x;
		}
		if(touch.phase == iPhoneTouchPhase.Ended){
			aPosX=bPosX;
		}
		if(aPosX!=bPosX){
			moveMag = (bPosX - aPosX)/moveSpeed;
		}else{
			moveMag = moveMag * speed;
		}
        if(moveMag >= magMaxPos){
        transform.Rotate(0, moveMag, 0);
        //RotateObject(transform, Vector3.up * moveMag, tSpeed);
        moveMag = magMaxPos;
		aPosX=bPosX;
        }
        else if(moveMag <= magMaxNeg){
        transform.Rotate(0, moveMag, 0);
        //RotateObject(transform, Vector3.up * moveMag, tSpeed);
        moveMag = magMaxNeg;
		aPosX=bPosX;
        }
    }
}

/*function RotateObject (thisTransform : Transform, degrees : Vector3, seconds : float) {
    var startRotation = thisTransform.rotation;
    var endRotation = thisTransform.rotation * Quaternion.Euler(degrees);
    var t = 0.0;
    var rate = 1.0/seconds;
    while (t < 1.0) {
        t += Time.deltaTime * rate;
        thisTransform.rotation = Quaternion.Slerp(startRotation, endRotation, t);
        yield;
    }
}*/

Ok I got it all fixed. Before I was trying to turn the camera based on how many degrees the swipe used, but I soon learned that that was just not going to cut it because it either lagged behind or it just wasn’t accurate, I then started to think of alternate methods and came up with one that works awesome. Basically you have an empty gameobject near the position of your character, then when a finger moves across the screen you capture its deltaPosition, and divide that by some amount (otherwise it jumps all over the place), you take the now divided touch.deltaPosition, and you add its .x to the empty gameobjects x position, and same for the y position. Then all you have to do is use a smooth lookat script and bam you have a great swipe to look aim script. If anyones interested I can post the final script here. The only problem is, is that this works more for a fps type game, so all I need to figure out how to do is pretty much reverse this process but clamp the rotation of the camera so it can only rotate around my player and on its x axis as this is a Dual stick shooter and I’m not using this for aiming. Thanks for all the help!

This is awesome!

You move the object and the camera look at it! Its so simple!

I know I couldn’t believe I hadn’t thought of it earlier.