Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Karsnen_2 · Sep 05, 2012 at 07:03 PM · c#iostouchswipe

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Oct 28, 2012 at 06:18 PM 0
Share

$$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

avatar image Karsnen_2 · Oct 29, 2012 at 01:26 PM 0
Share

Thanks Fattie!!! I think it would be worth for me.

3 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image namekaw · Sep 08, 2013 at 10:00 PM 0
Share

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!

avatar image kmeboe · Sep 08, 2013 at 10:44 PM 0
Share

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.

avatar image hgaur725 · Jun 09, 2014 at 09:13 AM 0
Share

@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).

avatar image hgaur725 · Jun 09, 2014 at 09:14 AM 0
Share

i wish u reply soon and sort out my problem thanks in advance @kmeboe

avatar image
1

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;
             }
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by pfonseca · Aug 12, 2014 at 11:04 PM

This post save my project: http://pfonseca.com/swipe-detection-on-unity/

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges