News: Enjoy our Christmas offer 30% off on our latest package Tower Defense Level Kit http://u3d.as/m7h

  • May 25, 2018, 01:15:59 AM

Login with username, password and session length

Author Topic: Twist AND Pinch functionality in same script?  (Read 6278 times)

Offline rxmarcus

  • Newbie
  • *
  • Posts: 28
Twist AND Pinch functionality in same script?
« on: April 02, 2015, 04:24:06 AM »
I am trying to allow the user to do both a 2 finger Twist and Pinch on a single object.

There are 2 example scripts that come with Easy Touch, one for "TwistMe" and one for "PinchMe", but there is no example script for a situation where you want to do both of these on the same object in the same script.  I'm not sure how to accomplish this because if you are looking at the "PinchMe" script it disables Twist with: "EasyTouch.SetEnableTwist( false);" and enables Pinch with: "EasyTouch.SetEnablePinch( true);".  Can you not have both enabled at the same time?

An example script of having both pinch and twist working together would be extremely helpful.

Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: Twist AND Pinch functionality in same script?
« Reply #1 on: April 02, 2015, 08:09:15 PM »
Hi,

Do you use EasyTouch 4 or EasyTouch 3 ?

Offline rxmarcus

  • Newbie
  • *
  • Posts: 28
Re: Twist AND Pinch functionality in same script?
« Reply #2 on: April 02, 2015, 10:55:52 PM »
I am using Easy Touch 4 in Unity 5.0

Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: Twist AND Pinch functionality in same script?
« Reply #3 on: April 03, 2015, 12:51:51 AM »
You have an example in UITwistPinch example, have you update with the last version of easytouch 4 ?

Offline rxmarcus

  • Newbie
  • *
  • Posts: 28
Re: Twist AND Pinch functionality in same script?
« Reply #4 on: April 03, 2015, 07:40:39 PM »
I do see that now. In my use its not on UI, so I'm probably ok to just change "gesture.pickedUIElement" to "gesture.pickedObject" on the example code right?

Also what do the drag and fingerId variables accomplish? Are those specific to using this code with UI elements?

Trying to impliment those code examples on my object I can't get simple dragging to work. IT seems UIDrag.cs doesn't even use the OnDrag event, so I'm confused on how drag even happens.

If its not a big deal it would be helpful for you to create a combined drag,pinch,twist script as part of the EasyTouch examples for standard game objects.
« Last Edit: April 03, 2015, 07:57:32 PM by rxmarcus »

Offline rxmarcus

  • Newbie
  • *
  • Posts: 28
Re: Twist AND Pinch functionality in same script?
« Reply #5 on: May 07, 2015, 06:04:11 AM »
I've played around with this and still can't get a good solution. Your example uses UI components, I'm trying to do a Drag, Pinch, and Twist combination on just a normal gameobject and can't get it to work. Could you please help me with this?  Here is my current script that I'm trying:

Code: [Select]
void Awake(){
myParent = transform.parent;
}

private int fingerId = -1;
private bool drag = true;

// Subscribe to events
void OnEnable()
{
// Drag Event Handlers
EasyTouch.On_TouchDown += On_TouchDown;
EasyTouch.On_TouchStart += On_TouchStart;
EasyTouch.On_TouchUp += On_TouchUp;
EasyTouch.On_TouchStart2Fingers += On_TouchStart2Fingers;
EasyTouch.On_TouchDown2Fingers += On_TouchDown2Fingers;
EasyTouch.On_TouchUp2Fingers += On_TouchUp2Fingers;

// Twist & Pinch Handlers
EasyTouch.On_Twist += On_Twist;
EasyTouch.On_Pinch += On_Pinch;

}



void OnDisable()
{
UnsubscribeEvent();
}

void OnDestroy()
{
UnsubscribeEvent();
}

void UnsubscribeEvent()
{
// Drag Event Handlers
EasyTouch.On_TouchDown -= On_TouchDown;
EasyTouch.On_TouchStart -= On_TouchStart;
EasyTouch.On_TouchUp -= On_TouchUp;
EasyTouch.On_TouchStart2Fingers -= On_TouchStart2Fingers;
EasyTouch.On_TouchDown2Fingers -= On_TouchDown2Fingers;
EasyTouch.On_TouchUp2Fingers -= On_TouchUp2Fingers;

// Twist & Pinch Handlers
EasyTouch.On_Twist -= On_Twist;
EasyTouch.On_Pinch -= On_Pinch;

}

void On_Pinch (Gesture gesture)
{
if (gesture.pickedObject == gameObject){
myParent.transform.localScale = new Vector3(transform.localScale.x +  gesture.deltaPinch * Time.deltaTime,  transform.localScale.y+gesture.deltaPinch * Time.deltaTime, transform.localScale.z );
}
}

void On_Twist (Gesture gesture){
if (gesture.pickedObject == gameObject){
myParent.transform.Rotate( new Vector3(0,0,gesture.twistAngle));
}
}

void On_TouchStart (Gesture gesture){

if (drag){
if (gesture.pickedObject == gameObject && fingerId==-1){
fingerId = gesture.fingerIndex;
myParent.transform.SetAsLastSibling();
}
}


}

void On_TouchDown (Gesture gesture){

if (fingerId == gesture.fingerIndex && drag){
if (gesture.pickedObject == gameObject ){
myParent.transform.position += (Vector3)gesture.deltaPosition;
}
}
}

void On_TouchUp (Gesture gesture){

if (fingerId == gesture.fingerIndex){
fingerId = -1;
}
}

void On_TouchStart2Fingers (Gesture gesture)
{
if (drag){
if ((gesture.pickedUIElement == gameObject || gesture.pickedUIElement.transform.IsChildOf( transform)) && fingerId==-1){
myParent.transform.SetAsLastSibling();
}
}
}



void On_TouchDown2Fingers (Gesture gesture)
{
if (gesture.isOverGui){
if (gesture.pickedObject == gameObject){
myParent.transform.position += (Vector3)gesture.deltaPosition;
drag = false;
}
}
}

void On_TouchUp2Fingers (Gesture gesture)
{
if (gesture.pickedObject == gameObject){
drag = true;
}
}

I'm referencing "myParent" because my collider along with this script are attached to a child gameobject and want to move / manipulate the parent object.

Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: Twist AND Pinch functionality in same script?
« Reply #6 on: May 08, 2015, 09:56:22 AM »
Can you tell me a little more.

What you do not like in what you do? Nothing works?Some works?

Offline rxmarcus

  • Newbie
  • *
  • Posts: 28
Re: Twist AND Pinch functionality in same script?
« Reply #7 on: May 09, 2015, 08:23:36 AM »
With that code that I posted nothing is working, I can't even drag. I'm wondering if you could build a script that allows drag, twist and pinch on a traditional gameobject like a cube primitive for example?  I just haven't been able to write a script that combines all 3 of these to work on normal gameobjects.

Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: Twist AND Pinch functionality in same script?
« Reply #8 on: May 10, 2015, 08:09:42 PM »
Ok, I will write an example

Offline rxmarcus

  • Newbie
  • *
  • Posts: 28
Re: Twist AND Pinch functionality in same script?
« Reply #9 on: May 12, 2015, 03:51:42 AM »
Thank you, that will be a big help.

Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: Twist AND Pinch functionality in same script?
« Reply #10 on: May 14, 2015, 11:06:57 AM »
Hi,

Here a little script, that allow you to turn on Z axis with twist, scale with pinch, and drag with one finger
Code: [Select]
public class MultiGestureExample : MonoBehaviour {

void OnEnable(){

EasyTouch.On_Twist += HandleOn_Twist;
EasyTouch.On_Pinch += HandleOn_Pinch;
EasyTouch.On_Drag += HandleOn_Drag;
}


void OnDisable(){
EasyTouch.On_Twist -= HandleOn_Twist;
EasyTouch.On_Pinch -= HandleOn_Pinch;
                EasyTouch.On_Drag -= HandleOn_Drag;
}


void HandleOn_Pinch (Gesture gesture){
if (gesture.pickedObject == gameObject){
transform.localScale = new Vector3(transform.localScale.x +  gesture.deltaPinch * Time.deltaTime,  transform.localScale.y+gesture.deltaPinch * Time.deltaTime, transform.localScale.z );
}
}


void HandleOn_Twist (Gesture gesture){

if (gesture.pickedObject == gameObject){
transform.Rotate( new Vector3(0,0,gesture.twistAngle));
}
}

void HandleOn_Drag (Gesture gesture){
if (gesture.pickedObject == gameObject && gesture.touchCount ==1){

transform.position = gesture.GetTouchToWorldPoint( transform.position);
}

}

}

Offline rxmarcus

  • Newbie
  • *
  • Posts: 28
Re: Twist AND Pinch functionality in same script?
« Reply #11 on: May 18, 2015, 03:51:25 AM »
Thank you for writing that code, however I still am have problems using it.
I am trying to use it on 2D images that have 2D polygon colliders, should that make any difference?
I recorded a video so you can see exactly what is happening. When I try to pinch or twist things go crazy.

Please see this video: https://youtu.be/EgzB9JSR1AY

Any more thoughts on what I'm doing wrong? I'm using your twist and pinch code exactly.

Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: Twist AND Pinch functionality in same script?
« Reply #12 on: May 18, 2015, 01:27:07 PM »
Hi,

If you look at my example, I do the drag only when the touchcount=1. I think that you don't do this test.


Offline rxmarcus

  • Newbie
  • *
  • Posts: 28
Re: Twist AND Pinch functionality in same script?
« Reply #13 on: May 19, 2015, 03:39:45 AM »
Ah looks like you were right, it is behaving much better when I do that test.
I notice with easy touch that during a gesture like twist or drag, if you go off the object accidentally that the object loses focus and you can no longer manipulate it until you let up your fingers and try again. For example if you are pinching to scale an object down and its so small that your fingers no longer fit on the object, or twist or pinching too fast.

Is this intentional? It makes it frustrating and difficult to twist and pinch something.
« Last Edit: May 19, 2015, 05:32:02 AM by rxmarcus »

Offline Nicolas

  • Administrator
  • Hero Member
  • *****
  • Posts: 792
Re: Twist AND Pinch functionality in same script?
« Reply #14 on: May 19, 2015, 04:00:44 PM »
Hi,

It depends on whether you enabled or not the  auto update , and the selection method for two-finger gesture :

Auto_update
On =  EasyTouch update the current picked objet at each frame
Off = Classical mode, EasyTouch detect the current picked objet only at touch start

pick method
finger = each finger must be over the object
average = it is the average position of the two finger