Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

what is Wrong in my Script ( c#) ?

Discussion in 'Scripting' started by m-y, Oct 1, 2014.

  1. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    471
    i wrote this script for android device
    so swapping right , left , jump and down
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BallMovement : MonoBehaviour {
    5.    
    6.  
    7.    
    8.    
    9.     public float  minswipedisty ;
    10.     public float minswipedistx   ;
    11.     private Vector2 startpos ;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.    
    16.    
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.  
    23.         if ( Input.touchCount > 0 )
    24.         {
    25.  
    26.             Touch touch  = Input.touches[0] ;
    27.  
    28.             switch ( touch.phase )
    29.             {
    30.             case TouchPhase.Began :
    31.                 startpos = touch.position  ;
    32.                 break  ;
    33.  
    34.  
    35.             case TouchPhase.Ended  :
    36.                 float swipe_dist_vertical  = (new Vector3(0,touch.position.y , 0 ) - new Vector3(0,startpos.y , 0 )).magnitude ;
    37.                 if ( swipe_dist_vertical >  minswipedisty )
    38.                 {
    39.  
    40.                     float Swipe_value  = Mathf.Sign ( touch.position.y - startpos.y ) ;
    41.                     if ( Swipe_value > 0 )
    42.                     {
    43.  
    44.                         print ("Jump") ;
    45.                         //transform.Translate(new Vector3(0,20,0) ) ;
    46.                     }
    47.                 else    if ( Swipe_value < 0 )
    48.                     {
    49.                         print ("Down") ;
    50.                     }
    51.                 }
    52.  
    53.                 float Swipt_dist_Horizontal  = (new Vector3 ( touch.position.x , 0 , 0 ) - new Vector3(startpos.x,0,0)).magnitude ;
    54.  
    55.                 if ( Swipt_dist_Horizontal >  minswipedistx )
    56.                 {
    57.                     float Swipe_Value  = Mathf.Sign(touch.position.x - startpos.x ) ;
    58.                     if ( Swipe_Value > 0 )
    59.                     {
    60.                         print ( "right") ;
    61.                         transform.Translate(new Vector3(20,0,0) ) ;
    62.                     }
    63.             else        if ( Swipe_Value < 0 )
    64.                     {
    65.  
    66.                         print ("Left") ;
    67.                         transform.Translate(new Vector3(-20,0,0) );
    68.                     }
    69.  
    70.                 }
    71.  
    72.  
    73.                 break ;
    74.  
    75.                 }
    76.  
    77.         }
    78.  
    79.     }
    80.  
    81. }
    82.  
    83.  
    84.  
    85.  
    86.  
    87.  
    88.  
    89.  
    90.  
    91.  
    92.  
    93.  
    94.  
    95.  
    96.  
    97.  
    98.  
    99.  
    100.  
    101.  
    102.  
    when i try to jump
    it is jumping and go to right at same time
    when i try to down
    it is down and go to left
    so what is missing here ?
     
  2. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    If there is an error here, I do not see it. Maybe you just need to increase minswipedistx? Perhaps you could print Swipt_dist_Horizontal to see how it compares to minswipedistx.

    Not related to your problem, but these lines:
    Code (csharp):
    1.  
    2. float swipe_dist_vertical  = (new Vector3(0,touch.position.y , 0 ) - new Vector3(0,startpos.y , 0 )).magnitude ;
    3. float Swipt_dist_Horizontal  = (new Vector3 ( touch.position.x , 0 , 0 ) - new Vector3(startpos.x,0,0)).magnitude ;
    4.  
    5.  
    Can be reduced to simply this:
    Code (csharp):
    1.  
    2. float swipe_dist_vertical  = touch.position.y - startpos.y;
    3. float Swipt_dist_Horizontal  = touch.position.x - startpos.x;
    4.  
     
  3. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    471
    ok i will try it
     
  4. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Maybe the problem is these lines:

    Code (csharp):
    1.  
    2. if ( swipe_dist_vertical >  minswipedisty )
    3.  
    4. //and
    5.  
    6. if ( Swipt_dist_Horizontal >  minswipedistx )
    7.  
    In the vertical direction this should work fine probably if swipe_dist_vertical is greater than 0, but what if the value is -0.000000001? It will swipe down. Same if Swipt_dist_Horizontal is -0.00000001. I would try using the absolute value of the swipe distance like this:

    Code (csharp):
    1.  
    2. if ( Mathf.abs(swipe_dist_vertical) >  minswipedisty )
    3.  
    4. //and
    5.  
    6. if ( Mathf.abs(Swipt_dist_Horizontal) >  minswipedistx )
    7.  
    8.  
     
  5. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Sorry, both of my posts were wrong. I misread your else if block. It's also clear now why you were using .magnitude. Trying something now, will post back in a bit.
     
  6. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Here, this might be a cleaner, easier way of doing it. I would use Vector2 instead of Vector3 because it's only a 2D swipe, but Vector3 should still work. I would also make minswipedistx and y a Vector2 normally, but have left it like yours.

    I included Mathf.Abs() functions so if someone accidentally uses negative values for minswipedistx and y it should still work. This logic is much easier to debug than the nested if/else if statements everywhere. Hope this helps.
    Code (csharp):
    1.  
    2. case TouchPhase.Ended  :
    3.  
    4.     Vector2 swipeValue = touch.position - startpos;
    5.  
    6.        if (swipeValue.y > Mathf.Abs(minswipedisty))
    7.        {
    8.            print("Jump");
    9.        }
    10.  
    11.        if (swipeValue.y < -Mathf.Abs(minswipedisty))
    12.        {
    13.            print("Down");
    14.        }
    15.  
    16.        if (swipeValue.x > Mathf.Abs(minswipedistx))
    17.        {
    18.            print("Right");
    19.        }
    20.  
    21.        if (swipeValue.x < -Mathf.Abs(minswipedistx))
    22.        {
    23.            print("Left");
    24.        }
    25.  
    26. break;
    27.  
     
  7. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    471
    Nothing Changed !
    Did You Test Ur code ? !
     
  8. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Which code are you talking about? I tested the last one by setting touch.position and startpos to various values to see what would show up in the print statements. That appeared to work. As for the previous posts, I already wrote that they were mistakes. Sorry about that, just trying to help.
     
  9. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    471
    when u try ur code if u moved ur finger to jump
    the editor print ( jump ) or it is print jump and right ? at same time ?
     
  10. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    As I said in the last post:

    "I tested the last one by setting touch.position and startpos to various values to see what would show up in the print statements. That appeared to work."

    What I mean is I set the variables directly in code (swipeValue.x and swipeValue.y) to simulate what should occur using this algorithm, then I observed the results of the print statements. It would only do both jump and right if swipeValue.x > Mathf.Abs(minswipedistx) and swipeValue.y > Mathf.Abs(minswipedisty). If the values were set such to simulate a jump swipe it only said "jump."

    Sorry, I did not make my own touch screen game today with a jumping character to test swipes on just to help you debug your code. This will have to do unless someone else comes along. ;)
     
  11. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You're moving into two directions because you check for both axis all the time.
    You either need to increase the limits in order to prevent small offsets from affecting the result, or you need to re-code the script (it's copied btw :p) and judge witch direction was the desired direction.
     
  12. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    471
    Waiting someone to solve the problem !!
     
  13. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Read the posts above. Have you even tried to fix that problem?
     
  14. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Two people have told you already how to fix it. Did you increase the limits yet or rewrite the code to be like mine?
     
  15. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    471
    i fixed it
    without any other code
     
  16. Todd-Wasson

    Todd-Wasson

    Joined:
    Aug 7, 2014
    Posts:
    1,079
    Great! What was wrong?
     
  17. m-y

    m-y

    Joined:
    Sep 22, 2013
    Posts:
    471
    1. public float minswipedisty public float minswipedistx ;
    2. i increased the minswipedisty value more than minswipedistx
    3. so i prevented the touch to mix the x axis with y
     
  18. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    No one but you and I noticed I guess :p