- Home /
swipe movement control
Hi there,
I am making a 2D game for touch devices but I'm having some trouble implementing touch controls. I've tried several things such as using Control Freak, coding swipes, etc. but I can't get it to work.
The kind of controls I am aiming for are similar to those in Mage Gauntlet & Wayward Souls, where the player swipes and holds in a direction, which the character gameObject follows. See https://www.youtube.com/watch?v=AjraDTy64BA 00:21 for what I mean.
Can anybody point me in the right direction & help me understand how controls like this work? Any tutorials are welcome! Here is my current script, which is from a tutorial & is not what I'm looking for.
void Update ()
{
float inputModifyFactor = (rigidbody2D.velocity.x != 0.0f && rigidbody2D.velocity.y != 0.0f) ? .7071f : 1.0f;
if (Input.touchCount > 0) {
foreach (Touch touch in Input.touches) {
switch (touch.phase) {
case TouchPhase.Began:
//This is a new touch
isSwipe = true;
fingerStartTime = Time.time;
fingerStartPos = touch.position;
break;
case TouchPhase.Canceled:
//The touch is being canceled
isSwipe = false;
break;
case TouchPhase.Ended:
float gestureTime = Time.time - fingerStartTime;
float gestureDist = (touch.position - fingerStartPos).magnitude;
if (isSwipe && gestureTime < maxSwipeTime && gestureDist > minSwipeDist) {
Vector2 direction = touch.position - fingerStartPos;
Vector2 swipeType = Vector2.zero;
if (Mathf.Abs (direction.x) > Mathf.Abs (direction.y)) {
//Horizontal swipe
swipeType = Vector2.right * Mathf.Sign (direction.x);
} else {
//Vertical swipe
swipeType = Vector2.up * Mathf.Sign (direction.y);
}
if (swipeType.x != 0.0f) {
if (swipeType.x > 0.0f) {
rigidbody2D.velocity = new Vector2 (10, rigidbody2D.velocity.y * inputModifyFactor);
} else {
rigidbody2D.velocity = new Vector2 (-10, rigidbody2D.velocity.y * inputModifyFactor);
}
}
if (swipeType.y != 0.0f) {
if (swipeType.y > 0.0f) {
rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x * inputModifyFactor, 10);
} else {
rigidbody2D.velocity = new Vector2 (rigidbody2D.velocity.x * inputModifyFactor, -10);
}
}
}
break;
}
}
}
}
Thank you so much!
Answer by alpha_waves · Feb 24, 2017 at 09:47 AM
As this is about 2 years later I'm guessing you've found your answer, but hey!
I'm trying to do Wayward Souls-style controls too for my own sci-fi action rpg
I found this tutorial where the programmer has done something similar to Wayward Souls...
https://www.youtube.com/watch?v=xP7zlw7o51M&t=29s
You can download his Unity project from GitHub in the links on that page.
I'm extending it to add in the right-hand-side swipes and some extra fun mechanics on top.
I would love to see what you ended up doing, and if you have any advice on this for me. Perhaps we could team up if you're interested? I've got lots of ideas for story, mechanics, music, and visuals - I'm just getting my feet wet in Unity at the moment trying to bring them to life and would love your help or to help you on a similar project :)
Your answer
Follow this Question
Related Questions
mouse input to touch input help please 0 Answers
How do you force points into a straight a line using swipe? 0 Answers
Swipe controls error "Cannot implicitly convert type 'UnityEngine.Touch' to 'Touch' " 1 Answer
Detect if finger lifted off screen 1 Answer
iPhone - How to calculate a decent touch swipe speed? 2 Answers