Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Swipe Gesture / Direction / Over an Object

Discussion in 'iOS and tvOS' started by FMIDaustin, Apr 15, 2009.

  1. FMIDaustin

    FMIDaustin

    Joined:
    Jan 18, 2009
    Posts:
    7
    Hello,

    I have a few questions for the Unity Masters out there. I am looking at doing a Swipe Gesture over a rigged tree branch that i have rigged up with IK from maya.

    First, i have used a touch phase, with Began, moved and ended, but what i wanted to know is how to just have that swipe motion be detected by just the one tree branch because in the end i am going to want to be able to swipe over all three different branches, will i have to do a ray cast from the point of the first touch and if it hits that tree, then just that tree grabs the movement?

    Second, I wanted to know if there is a simple way to grab the direction of the Swipe, because i really want it in the end to have the direction of the swipe control the wind (velocity) in the direction of where i touched. Here is a little bit of code that i wrote for the swipe, if anyone could see if its going to work, or if im going in the wrong direction.

    thanks

    Dan



    Code (csharp):
    1.  
    2.  
    3. var Wind : GameObject;
    4. var startPosX = 0;
    5. var endPosX = 0;
    6. var startPosZ = 0;
    7. var endPosZ = 0;
    8. function Update () {
    9.  
    10. for (var event : iPhoneTouch in iPhoneInput.touches){
    11.        
    12.         if (event.phase == iPhoneTouchPhase.Began){
    13.             startPosX = event.position.x;
    14.             startPosZ = event.position.y;
    15.             //print(startPosX);
    16.         }
    17.        
    18.         if (event.phase == iPhoneTouchPhase.Ended){
    19.             endPosX = event.positionDelta.x;
    20.             endPosZ = -event.positionDelta.y;
    21.            
    22.             //print(startPos + " : " + endPos);
    23.             if(startPosX > 0){
    24.             print ("Negitive Direction");
    25.             rigidbody.velocity.x = -0.2;
    26.            
    27.             if(endPosX > 0){
    28.             print ("Positive Direction");  
    29.             rigidbody.velocity.x = 0.2;
    30.            
    31.             if(startPosZ > 0){
    32.             print ("Positive Direction Z");
    33.             rigidbody.velocity.z = 0.2;
    34.            
    35.             if(endPosZ > 0){
    36.             print ("Negative Direction Z");
    37.             rigidbody.velocity.z = 0.2;
    38.            
    39.            
    40.                         }
    41.                     }
    42.                 }  
    43.             }                              
    44.         }
    45.     }
    46. }
    47.  
     
  2. CedarParkDad

    CedarParkDad

    Joined:
    May 28, 2008
    Posts:
    98
    The way I would probably do it is by creating a series of explosions on the side the wind is coming from. This will catch any branches with Rigidbody components and move them for you. The reason to make it a series with some variability is to create that illusion of non-constancy of a wind. You find the explanation and an example to get every Rigidbody within the explosion here online:

    http://unity3d.com/support/documentation/ScriptReference/Rigidbody.AddExplosionForce.html

    You can also find it in your own documentation under Rigidbody.
     
  3. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    Hi, all,

    Would either of you have a solution for a swipe-to-move camera script? I have been searching and experimenting for a while now, but haven't had much success.

    Thanks,

    Greg
     
  4. frozenpepper

    frozenpepper

    Joined:
    Sep 17, 2007
    Posts:
    219
    HI, here is a script I use to detect left and right "swipes" in the upper or bottom part of the Iphone screen (vertical horientation), hope it helps...
    Code (csharp):
    1. var touch1 : float;
    2. var touch2 : float;
    3. var touch3 : float;
    4. var bloccoKO : TouchControllerRag;
    5. var detonatore : Transform;
    6.  
    7. var regular = 1;
    8. var bloccoSgambetto = 0;
    9.  
    10. var mov = 0.00;
    11. var touchActive = 0;
    12. function Start() {
    13.    
    14.     regular = 1;
    15. }
    16.  
    17. function Update () {
    18.    
    19.    
    20.  
    21.    if (touchActive <1) {
    22.    
    23.    for(touch in iPhoneInput.touches){
    24.    
    25. if (regular ==1) {  
    26.    
    27.    
    28.        
    29.       if(touch.phase == iPhoneTouchPhase.Began) {
    30.         touch1 = touch.position.x;
    31.         touch3 = touch.position.y;
    32.       }
    33.        if(touch.phase == iPhoneTouchPhase.Moved) {
    34.         touch2 = touch.position.x;
    35.         if ( touch3 <220) {                      //lower part of the screen
    36.            
    37.         if (touch2 - touch1 > 70 ) {
    38.                 bloccoSgambetto = 1;
    39.                 bloccoKO.SgambettoDes();
    40.       }
    41.         if (touch1 - touch2 > 70 ) {
    42.                 bloccoSgambetto = 1;
    43.                 bloccoKO.SgambettoSin();
    44.       }
    45.  
    46.         }
    47. if ( touch3 >280) {  //upper part of the screen
    48. if (touch2 - touch1 > 40 ) {
    49.     bloccoKO.SchiaffoDestra();
    50. }
    51. if (touch1 - touch2 > 40 ) {
    52.     bloccoKO.SchiaffoSinistra();
    53. }
    54.  
    55.  
    56. }  
    57.         }
    58.  
    59.      }
    60.      
    61.      
    62. }
    63.      
    64.      
    65.       }
    66.  
    67.    }
    68.            
    69. }
    while copyng and deleting the totally useless parts of my code might have lost some brackets or left to many but the basics of swipes gestures are here....
     
  5. Holocene

    Holocene

    Joined:
    Feb 21, 2009
    Posts:
    347
    Thanks, taio84! I appreciate you posting that. I think it will be a good place to start.
    Thanks,

    Greg
     
  6. FMIDaustin

    FMIDaustin

    Joined:
    Jan 18, 2009
    Posts:
    7
    THANK you all so much, i will give this a try tomorrow, and now i see where i was going wrong....

    again thanks so much
     
  7. bbvrdev

    bbvrdev

    Joined:
    Aug 11, 2009
    Posts:
    221
    I just wanted to pipe in and say thanks to taio84 also. This totally helped me as well!
     
  8. groovfruit

    groovfruit

    Joined:
    Apr 26, 2010
    Posts:
    257
    Well, I feel like I'm missing something.....

    In the above script, what does "var bloccoKO : TouchControllerRag; " refer to? I get an error on this line ... Am I missing something?
     
  9. Cawas

    Cawas

    Joined:
    Jan 14, 2010
    Posts:
    121
    Thanks for this. It helped a lot!