- Home /
Detect the swipe and add force respective to it?
Hello,
I am trying to detect a swipe on the iPad and apply a force in its concerned direction and length(magnitude). So I just take the input of the swipes that are linear and then use them as the input. I, however did script but I did not bring in the desired.
CODE
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
{
final = Vector3.zero;
length = 0;
SW = false;
Vector2 touchDeltaPosition = Input.GetTouch(0).position;
startpos = new Vector3(touchDeltaPosition.x,0,touchDeltaPosition.y);
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Moved)
{
SW = true;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Canceled)
{
SW = false;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Stationary)
{
SW = false;
}
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Ended)
{
if(SW)
{
Vector2 touchPosition = Input.GetTouch(0).position;
endPos = new Vector3(touchPosition.x,0,touchPosition.y);
final = endpos - startpos;
length = final.magnitude;
}
}
Can you guys help me on this issue?
THANK YOU
$$anonymous$$ars -- here is an incredibly long discussion, concerning some details of this issue. It may be relevant in some way (or maybe not!)
http://answers.unity3d.com/questions/292333/how-to-calculate-swipe-speed-on-ios.html
Answer by kmeboe · Sep 05, 2012 at 08:08 PM
I created a new project using your code, and everything seemed to work properly. You did have one syntax error (typo on "endPos", which should be "endpos"), but other than that everything looked fine.
I simply created an empty project with the following script:
using UnityEngine;
using System.Collections;
public class pooh : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
private float length = 0;
private bool SW = false;
private Vector3 final;
private Vector3 startpos;
private Vector3 endpos;
// Update is called once per frame
void Update ()
{
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)
{
final = Vector3.zero;
length = 0;
SW = false;
Vector2 touchDeltaPosition = Input.GetTouch (0).position;
startpos = new Vector3 (touchDeltaPosition.x, 0, touchDeltaPosition.y);
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Moved)
{
SW = true;
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Canceled)
{
SW = false;
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Stationary)
{
SW = false;
}
if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Ended)
{
if (SW)
{
Vector2 touchPosition = Input.GetTouch (0).position;
endpos = new Vector3 (touchPosition.x, 0, touchPosition.y);
final = endpos - startpos;
length = final.magnitude;
}
}
}
void OnGUI()
{
GUI.Box (new Rect (50,300,500,30), "length: " + length);
}
}
The script is called "pooh.cs". Don't worry, it's not dirty. This is like Winnie the Pooh.
Anyway, I then attached the pooh script to the "Main Camera" object. Everything works like you've coded it, with the "Length" display updating after each swipe.
If the steps I've included don't help you, maybe you can explain the exact bug you're seeing.
-Kevin
Sorry for asking a silly question but how do I attach this to a gameobject. I know you attach the script to the main camera, but how does it know what to add the force to. Thanks!
There are no silly questions; only silly people.
I joke, I joke. In reality all questions are silly.
Is that enough random one-liners? $$anonymous$$oving on.
The above script simply captures a swipe, then displays the length on the screen (via GUI.Box). If you'd like for the swipe to affect an object, then you'll need to provide a reference of that object to this script, and hook it up. Here's one way to do it:
1) Create a GameObject with a rigid body (http://docs.unity3d.com/Documentation/Components/class-Rigidbody.html) and place it in your scene. A rigid body can be attached by selecting the object you've created and choosing "Physics->Rigidbody" from the "Component" menu. 2) In the script above, add a GameObject reference (for instance, below the "Private vector3 endPos" line). Something like this: public GameObject automobile; 3) In Unity, click on your camera in the "Hierarchy" tab (the one that has the above script attached to it). You should see the script show up in the inspector, and should also see "Automobile" as an empty object on the script. 4) Drag the object from step 1 (located in the "Hierarchy" tab) onto "Automobile". You should see the reference change. 5) That's it! You should now be able to affect the object's rigidbody, moving it around.
Note: this isn't necessarily the best way to do things, depending on what you're looking for. But it should work.
@kmeboe: i had implemented this logic which u had written up .. i attached the script on football(sphere). but i am having some issues 1 : i want touch or flick or swipe work when i touch the Football not on wholescreen as this script does. 2 : the touch is behaving too weird as it is swiping ball in the right side only with too much of force as i added rigidbody.addforce(final).
i wish u reply soon and sort out my problem thanks in advance @kmeboe
Answer by Nomibuilder · Jan 24, 2015 at 01:56 PM
using UnityEngine;
using System.Collections;
public class SwipeDetector : MonoBehaviour
{
public float minSwipeDistY;
public float minSwipeDistX;
private Vector2 startPos;
void Update()
{
//#if UNITY_ANDROID
if (Input.touchCount > 0)
{
Touch touch = Input.touches[0];
switch (touch.phase)
{
case TouchPhase.Began:
startPos = touch.position;
break;
case TouchPhase.Ended:
float swipeDistVertical = (new Vector3(0, touch.position.y, 0) - new Vector3(0, startPos.y, 0)).magnitude;
if (swipeDistVertical > minSwipeDistY)
{
float swipeValue = Mathf.Sign(touch.position.y - startPos.y);
if (swipeValue > 0)//up swipe
//Jump ();
else if (swipeValue < 0)//down swipe
//Shrink ();
}
float swipeDistHorizontal = (new Vector3(touch.position.x,0, 0) - new Vector3(startPos.x, 0, 0)).magnitude;
if (swipeDistHorizontal > minSwipeDistX)
{
float swipeValue = Mathf.Sign(touch.position.x - startPos.x);
if (swipeValue > 0)//right swipe
//MoveRight ();
else if (swipeValue < 0)//left swipe
//MoveLeft ();
}
break;
}
}
}
}
Answer by pfonseca · Aug 12, 2014 at 11:04 PM
This post save my project: http://pfonseca.com/swipe-detection-on-unity/
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Multi Touch Drag Screen To Move Player? 1 Answer
IOS touch vs OnMouseDown & error messages 0 Answers
Detect swipe up gesture IOS 1 Answer
Make Swipe Detection Longer? 0 Answers