I’ve been trying to figure out how to capture a swipe movement without good success.
Lets say I have 3 “windows” or “screens”. The iphone only shows one at a time. To go to the next screen I swipe my finger to the left. The swipe needs to be across X pixels. (meaning you need to have swiped across a certain amount of pixels before it goes to the next page). Also if your swipe is less than x pixels, I want the window to move slightly, but then move back into place when the finger is lifted up.
These features i’m talking about are how standard iphone apps typically work, very fluid. However it seems like achieving this in unity is a bit of work.
I’m trying to help but need to understand what the issue is first. Maybe you can help me understand what part of it seems like a bit of work?
I ask because you can detect when a touch first happens and when it releases. Given this, you can determine the distance between the two events and divide by the screen width to figure out how far they moved in relation to the resolution.
How do I determine the distance between the touch events? With the games i’ve made, I’ve only ever used iPhoneTouchPhase.Began/Ended/Stationary. If that is how I check it can you please show me some example code?
Hello marklennartprice. Just read your post here.
I am too stuck with the SWIPE gesture problem.
Did you find anything concrete to help you out of this problem.
If you really do then could please provide me the script to do this SWIPE gesture cuz i have been trying to do this for 3days but still not able to figure it out.
Please help me!
Thanks.
I tried this coding for SWIPE gesture. Please go through it. I may be long but please go through it for once.
using UnityEngine;
using System.Collections;
public class swipe : MonoBehaviour {
void Start ()
{
sphere=GameObject.Find("Earth");
}
GameObject sphere;
Vector3 temp,pos,start,end;
Ray ray;
static float starttime,endtime;
static bool touched=false;
void Update ()
{
foreach(iPhoneTouch touch in iPhoneInput.touches)
{
temp=new Vector3(touch.position.x,touch.position.y,4);
ray=camera.ScreenPointToRay(temp);
pos=camera.ScreenToWorldPoint(temp);
if(touch.phase==iPhoneTouchPhase.Began)
{
if(Physics.Raycast(pos,Vector3.up ,0.38f))//To get whether the touch is on Object
{
starttime=Time.time;
start=sphere.transform.position;
touched=true;//To call in the Ended phase that the object was touched
sphere.rigidbody.Sleep();//To reduce the force on this rigidbody
}
}//end of Began
if(touch.phase==iPhoneTouchPhase.Moved)
{
if(Physics.Raycast(pos,Vector3.up ,0.38f))
{
sphere.transform.position=pos;
start=sphere.transform.position;
}
}//end of Moved
if(touch.phase==iPhoneTouchPhase.Ended)
{
/*Last position of touch*/
end= camera.ScreenToWorldPoint(new Vector3(touch.position.x,touch.position.y,4));
if(endtime-starttime<=2 touched==true)//a particular time to get swipe gesture
{
Vector3 pos1=new Vector3(end.x-start.x,end.y-start.y,4);
sphere.rigidbody.AddForce(pos1*50f);
touched=false;
}
}//end of Ended
}//end of foreach
}//end of update()
}//end of class
Actually i provided the motion in the object via AddForce of the rigidbody but i think it must be implemented in other ways also like Vector3.Lerp to give a smooth and specified distance.
And there is some problem with AddForce functionality that it keeps on adding the force. I do have provided the rigidbody.Sleep() but i don’t like the type of functionality it is actually giving. I want the swipe gesture like that of Spider: The Secret of Bryce Manor.
I like the swipe provided by them in the game.
Could anyone please help me in this?
For better reference please find the script in the attachment.
Thanks
SwipeControl looks great, I might even get it. However, how does it work with draw calls? I use EZ GUI/Sprite Manager for all of my images, and I can’t have 1 draw call for every swipe image (that mean mean 50+ calls at one time).
Do you have a more basic version of the code you posted? Its a little overwhelming and looks like its using members of a parent class that I’m not sure what they do.
Shouldn’t this swipe be as simple as calculating the distance/direction of the swipe and then doing something like: camera.transform.position += theDistance ??
My problem is I’m not sure how to capture the distance of the swipe.