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))
{
}
}
}*/
}
Your answer
Follow this Question
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