- Home /
Finger Manager position
Hi,
I'm using the FingerManager script found on Unitywiki and it works great, but I need to get the touch position and this script is only sending touch moves, began and cancels. Any suggestions on how to do that? Code below is .cs and I use void FingerBegin (Touch evt) { from a gameobject with a collider to see when a button is pressed. This works with multitouch....Thx for any help!
using UnityEngine; using System.Collections;
public class Finger { public Touch touch; public bool moved = false; public ArrayList colliders = new ArrayList(); }
public class FingerManager : MonoBehaviour {
public ArrayList fingers = new ArrayList();
void Update () { RaycastHit[] hits; foreach (Touch evt in Input.touches) { // if (evt.phase==TouchPhase.Began) {
if (evt.phase==TouchPhase.Began || evt.phase==TouchPhase.Moved ) {
Finger finger = new Finger();
finger.touch = evt;
Ray ray = Camera.main.ScreenPointToRay(evt.position);
hits = Physics.RaycastAll(ray);
foreach (RaycastHit hit in hits) {
finger.colliders.Add(hit.collider);
GameObject to = hit.collider.gameObject;
to.SendMessage("FingerBegin",evt,SendMessageOptions.DontRequireReceiver);
}
fingers.Add(finger);
}
else if (evt.phase==TouchPhase.Moved) {
for (int i=0;i<fingers.Count;++i) {
Finger finger = (Finger)fingers[i];
if (finger.touch.fingerId==evt.fingerId) {
finger.moved = true;
foreach (Collider collider in finger.colliders) {
if (collider==null) {continue;}
GameObject to = collider.gameObject;
to.SendMessage("FingerMove",evt,SendMessageOptions.DontRequireReceiver);
}
}
}
}
else if (evt.phase==TouchPhase.Ended || evt.phase==TouchPhase.Canceled) {
Ray ray = Camera.main.ScreenPointToRay(evt.position);
hits = Physics.RaycastAll(ray);
for (int i=0;i<fingers.Count;) {
Finger finger = (Finger)fingers[i];
if (finger.touch.fingerId==evt.fingerId) {
foreach (Collider collider in finger.colliders) {
if (collider==null) {continue;}
bool canceled = true;
foreach (RaycastHit hit in hits) {
if (hit.collider==collider) {
canceled = false;
GameObject to = collider.gameObject;
to.SendMessage("FingerEnd",evt,SendMessageOptions.DontRequireReceiver);
}
}
if (canceled) {
GameObject to = collider.gameObject;
to.SendMessage("FingerCancel",evt,SendMessageOptions.DontRequireReceiver);
}
}
fingers[i] = fingers[fingers.Count-1];
fingers.RemoveAt(fingers.Count-1);
}
else {
++i;
}
}
}
}
} }
Next time you post code, please use the 10101 button up top to format it correctly =). Thanks a lot!
Answer by loopyllama · Mar 30, 2011 at 03:28 PM
evt is your Touch, so evt.position is your touch position, which is a vector2 so something like:
Vector2 pos = evt.position;
just send pos in a message with SendMessage which is documented with an example here: http://unity3d.com/support/documentation/ScriptReference/GameObject.SendMessage.html
@ Harmless Ahhh, Thank You.
@JW, Will do, where is that button though?
Hi there, I added Vector2 pos = evt.position; and then changed the send message line as: to.Send$$anonymous$$essage("FingerBegin",pos,Send$$anonymous$$essageOptions.DontRequireReceiver);
$$anonymous$$y other script should receive the message with: void FingerBegin ( Vector2 pos) { print(pos); } But when it runs, I get a: $$anonymous$$issing$$anonymous$$ethodException: The best match for method FingerBegin has some invalid parameter error every time I touch using Unity Remote. Whaaaaaa...:(
Any tips? Oooh why oh why does multi-touch have to be so complicated? Peter
Still waiting for an answer from some kind gentle sir or madame.
it sounds like you may have this script somewhere else as it is sending FingerBegin the touch evt, and this version which sends FingerBegin a Vector2. In any case you can leave the original code intact and have another game object get the message with touch evt, then simply access the position from the evt with evt.position...
Your answer
Follow this Question
Related Questions
C# script for unity camera. Detect difference between two finger swipe and pinch to zoom? 2 Answers
Problem in Disabling Multi touch 0 Answers
MultiTouch issues 1 Answer
Go from mouse to multitouch? 1 Answer
Windows 10 multitouch test in editor 0 Answers