- Home /
Swipe horizontally and hold finger down to move character?
Hi!
Working on my first project, and I hit a wall with my character controller. The game is an ios Mobile 2D side scroller.
The mechanic has a few parameters.
If the player swipes left to right, and keeps finger down, and the swipe speed is slow enough, the character walks. But if the swipe speed is fast enough, they run. If the player swipes back to the left, with all the same parameters, the character walks or runs to the left depending on swipe speed.
Below is as far as I could get with my limited scripting knowledge and online research. I succeeded in moving the character left and right with the swipe but here are my problems. - Character shakes as it moves. - The finger stationary statement calls the run speed no matter if the finger arrived there on a slow swiping speed.
You can drop the script on an object and observe what I'm seeing. Thank you for any help, and if you have time, please explain what you're doing to make it work. I really want to get better at this.
public class secondTest : MonoBehaviour
{
private Vector2 fingerDown;
private Vector2 fingerUp;
public float walk = 2f;
public float run = 10f;
public float walkRunThreshold = 5f;
// Update is called once per frame
void Update()
{
foreach (Touch touch in Input.touches)
{
float swipeSpeed = touch.deltaPosition.magnitude / touch.deltaTime;
if (touch.phase == TouchPhase.Began)
{
fingerUp = touch.position;
fingerDown = touch.position;
}
//Detects Swipe while finger is still moving
if (touch.phase == TouchPhase.Moved && swipeSpeed >= walkRunThreshold)
{
fingerDown = touch.position;
if (fingerDown.x - fingerUp.x > 0)
{
WalkRight ();
}
else if (fingerDown.x - fingerUp.x < 0)
{
WalkLeft ();
}
}
else if (touch.phase == TouchPhase.Moved && swipeSpeed < walkRunThreshold)
{
fingerDown = touch.position;
if (fingerDown.x - fingerUp.x > 0)//Right swipe
{
RunRight();
}
else if (fingerDown.x - fingerUp.x < 0)//Left swipe
{
RunLeft();
}
}
if (touch.phase == TouchPhase.Stationary && swipeSpeed >= walkRunThreshold)
{
fingerDown = touch.position;
if (fingerDown.x - fingerUp.x > 0)
{
WalkRight ();
}
else if (fingerDown.x - fingerUp.x < 0)
{
WalkLeft ();
}
}
else if (touch.phase == TouchPhase.Stationary && swipeSpeed < walkRunThreshold)
{
fingerDown = touch.position;
if (fingerDown.x - fingerUp.x > 0)//Right swipe
{
RunRight();
}
else if (fingerDown.x - fingerUp.x < 0)//Left swipe
{
RunLeft();
}
}
}
}
void WalkLeft()
{
transform.Translate(Vector2.left * Time.deltaTime * walk);
}
void WalkRight()
{
transform.Translate(Vector2.right * Time.deltaTime * walk);
}
void RunLeft()
{
transform.Translate(Vector2.left * Time.deltaTime * run);
}
void RunRight()
{
transform.Translate(Vector2.right * Time.deltaTime * run);
}
}
Answer by jckmull86 · Mar 04, 2018 at 11:06 PM
Hello! I'm still looking for help on this one. Help doesn't have to be fixing my code. If anyone knows of a great all encompassing guide on coding touch gestures, or maybe an asset store game with similar touch features. Something I could dissect.
This is my first question ever so I apologize if I didn't follow the forum guidelines correctly. I'll take any suggestions towards making future inquiries more visible to the community.
Thanks!
Hi sry to say this but not many ppl answer questions here. $$anonymous$$y advise to you is : 1. If you cant find what you are looking for try describing your question in other ways. 2. If you still cant find it drop the project do something other.
btw if you are looking for a movement for a specific game try typing the name of game and its movement maybe it helps.
out of curiosity do you want to make a game like Temple Run? try typing it in google good luck.
Well I really appreciate you replying at least. I hadn't tried typing in a game with similar controls. That will take some digging because I don't know of any, but it's still helpful.
No it not like temple run. It's a platformer like Ninja Gaiden from the NES days. But I don't want to use a touch controller pad with UI buttons and a joystick. Just swipes and taps on the screen. Thanks again!
Your answer
Follow this Question
Related Questions
Check whether touch is held 0 Answers
Custom touch phase? 0 Answers
Swipe Control Issue.. Need help 1 Answer
TouchPhase doesn't end when player swipes off screen 1 Answer