Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by LinkWindcrafter · Jul 11, 2016 at 12:26 AM · touchasset storetouch controlstouches

TouchScript Library - Continually (by frame or change of position) get touch position

Hi. Well, I am making a script that detects a circular swipe, and I think the best way to go would be to continually check a finger (or TouchPoint) position, calculate it's magnitude and angle relative to a center reference, etc.

The problem is, that I haven't found yet a solution to successfully detect by each frame the position of the same finger, I only can get it's position by the event handlers, that are activated OnEnabled, which is called when you click/touch. I tried to subscribe to a event handler on a FixedUpdate, but it doesn't seem to make the deal. I have read the API extensively (which it isn't much, unfortunately, the documentation doesn't have much reference examples, not at least for someone like me that is still learning). I dunno if there is something that I haven't tried yet that you could know, or maybe I am doing it with the incorrect approach.

Here is the code, and thanks in advance:

 using UnityEngine;
 using System.Collections;
 using TouchScript;
 using TouchScript.Gestures;
 using TouchScript.Hit;
 using System;
 using DG.Tweening;
 using System.Collections.Generic;
 
 public class CircularSwipe : MonoBehaviour
 {
     bool holded; //Flag that activates if a press is being held, and that deactivates once it is released.
 
     GameObject senderReference;
 
     //Store the components that will form the vector of the radius of the circle with their respective components.
     public GameObject[] radiusPositions = new GameObject[2];
 
     float magnitude; //Stores the radius magnitude, for circle swipe on run-time calculations
     float initialAngle; //Stores the initial angle of the circle swipe request.
     float deltaX;
     float deltaY;
 
     void Awake()
     {
         //By no modifyng the original script, we change the time to detect longPressHandler gesture...
         GetComponent<LongPressGesture>().TimeToPress = 0.5f;
 
         holded = false;
 
         //Get both x and y components of the vector between both swipe components...
         deltaX = Mathf.Abs(radiusPositions[0].transform.position.x - radiusPositions[1].transform.position.x);
         deltaY = Mathf.Abs(radiusPositions[0].transform.position.y - radiusPositions[1].transform.position.y);
 
         calculateMagnitude(deltaX, deltaY);
 
         //The angle that will determine the range of the angle requested for the swipe.
         initialAngle = Mathf.Atan2(deltaY, deltaX) * Mathf.Rad2Deg;
 
         Debug.Log("Delta X equals to: " + deltaX);
         Debug.Log("Delta Y equals to: " + deltaY);
         Debug.Log("Angle equals to: " + initialAngle);
         Debug.Log("Magnitude equals to: " + magnitude);
     }
 
     //Add both PressGesture and ReleaseGesture scripts to the object...
     private void OnEnable()
     {
         GetComponent<PressGesture>().Pressed += pressedHandler;
         //GetComponent<LongPressGesture>().LongPressed += longPressedHandler;
         GetComponent<LongPressGesture>().StateChanged += longPressedHandler;
         GetComponent<ReleaseGesture>().Released += releaseHandler;
 
         if (TouchManager.Instance != null)
         {
           TouchManager.Instance.TouchesBegan += touchesBeganHandler;
         }
     }
 
     private void OnDisable()
     {
         GetComponent<PressGesture>().Pressed -= pressedHandler;
         //GetComponent<LongPressGesture>().LongPressed -= longPressedHandler;
         
         GetComponent<ReleaseGesture>().Released -= releaseHandler;
 
         if (TouchManager.Instance != null)
         {
           TouchManager.Instance.TouchesBegan -= touchesBeganHandler;
         }
     }
 
     private void pressedHandler(object sender, EventArgs e)
     {
         holded = true;
         //GetComponent<LongPressGesture>().LongPressed += longPressedHandler;
 
         PressGesture gesture = sender as PressGesture;
         TouchHit hit;
         gesture.GetTargetHitResult(out hit);
         RaycastHit rayCast = hit.RaycastHit;
 
         if(rayCast.transform.gameObject == this.radiusPositions[1])
         {
             Debug.Log("Yup, started where it had to...");
             GetComponent<LongPressGesture>().StateChanged -= longPressedHandler;
         }
         else
         {
             Debug.Log("Nope, didn't start where it had to...");
         }
     }
 
     private void releaseHandler(object sender, EventArgs e)
     {
         holded = false;
         //GetComponent<LongPressGesture>().LongPressed -= longPressedHandler;
     }
 
     private void longPressedHandler(object sender, GestureStateChangeEventArgs e)
     {
         LongPressGesture gesture = sender as LongPressGesture;
         TouchHit hit;
         gesture.GetTargetHitResult(out hit);
 
         if(e.State == Gesture.GestureState.Recognized) //Gesture recognized, hence it is on gameObject the script is attached to...
         {
         }
     }
 
     private void touchesBeganHandler(object sender, TouchEventArgs e)
     {
         int currentID = 0;
 
         float x;
         float y;
         
         foreach(TouchPoint point in e.Touches)
         {
             currentID = point.Id;
             Debug.Log(point.Position);
             Debug.Log("Also with ID: " +point.Id);
             x = point.Position.x;
             y = point.Position.y;
         }
 
         Debug.Log("Outside the foreach: " + e.Touches[currentID]);
         Debug.Log("e of type: " +e.GetType());
 
         calculateTrayectory(currentID);
 
         //Debug.Log("Event: " + e.TouchEventArgs);
     }
 
     void calculateMagnitude(float x, float y)
     {
         magnitude = Mathf.Sqrt((Mathf.Pow(x,2)+Mathf.Pow(y,2)));
     }
 
     void FixedUpdate()
     {
 
         if(holded)
         {
             if (TouchManager.Instance != null)
             {
                   TouchManager.Instance.TouchesBegan += touchesBeganHandler;
             }
         }
     }
 
     void calculateTrayectory(int id)
     {
         Debug.Log("Touch: ");
     }
 
     /*void calculateCurrentPoint(float x, float y)
     {
         float tempMagnitude;
         float tempAngle;
 
         tempMagnitude = Mathf.Sqrt((Mathf.Pow(x,2)+Mathf.Pow(y,2)));
         tempAngle = Mathf.Atan2(y, x) * Mathf.Rad2Deg;
 
         if(tempMagnitude >= (magnitude - 20) || tempMagnitude <= (magnitude + 20))
         {
             if(tempAngle >= (initialAngle -20) || tempAngle <= (initialAngle + 20))
             {
     
             }
         }
     }*/
 }
 
Comment
Add comment
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

0 Replies

· Add your reply
  • Sort: 

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

73 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Script to run on tap, exempt in area. 0 Answers

Error CS1026: Unexpected symbol ;, expecting ) 0 Answers

Best tool to do swipe/touch control of multiple objects in one scene 0 Answers

OnScreen-Stick that moves to the position touched 0 Answers

Is it possible to detect touch In a certain GUI element? 1 Answer


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