Search Unity

double click game object not working as expected

Discussion in 'Scripting' started by Gdakkutlu, Apr 17, 2019.

  1. Gdakkutlu

    Gdakkutlu

    Joined:
    Nov 9, 2016
    Posts:
    47
    Hey all,

    I've been recently trying to add double and single click to object.
    Well, obviously I couldn't succeed it.
    Here is my code.
    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.  
    5.  
    6.     public class jumper : MonoBehaviour {
    7.  
    8.         public GameObject obj;
    9.         Rigidbody rb;
    10.         public bool isGrounded;
    11.     public float collisionTime = 0;
    12.  
    13.     public float timeDelay =0;
    14.     public int timeTotal =0;
    15.  
    16.         void Start ()
    17.         {
    18.             rb = GetComponent<Rigidbody>();
    19.         }
    20.  
    21.      
    22.  
    23.             private void OnCollisionStay()
    24.         {
    25.                     isGrounded = true;
    26.                     StartCoroutine("stayTime");
    27.         }
    28.  
    29.             IEnumerator stayTime()
    30.             {
    31.                     yield return new WaitForSeconds(6f);
    32.                     Destroy(obj);
    33.  
    34.             }
    35.  
    36.        
    37.    
    38.         void OnCollisionExit()
    39.         {                    
    40.             StopCoroutine("stayTime");
    41.             isGrounded = false;
    42.         }
    43.  
    44.  
    45.         void Update ()
    46.         {
    47.        
    48.             if (Input.touchCount > 0 && isGrounded== true)
    49.             {
    50.                 for(int i =0; i < Input.touchCount; i++)
    51.                 {
    52.                 Touch touch = Input.GetTouch(i);
    53.                 timeTotal+=1;
    54.                 Debug.Log(timeTotal);
    55.  
    56.                
    57.                 if (touch.position.x < Screen.width/2 && (timeDelay < 0.5) && (timeTotal == 2))
    58.                 {
    59.                     if(pathManager.randomDirection == 0)
    60.                              {
    61.                                     rb.AddForce(new Vector3(-4, 5, 4), ForceMode.Impulse);
    62.                                     rb.velocity = Vector3.zero;
    63.                                     timeTotal = 0;
    64.                                     timeDelay =0;
    65.                              }
    66.                      else if(pathManager.randomDirection == 1)
    67.                              {
    68.                                     rb.AddForce(new Vector3(-4, 5, 4), ForceMode.Impulse);
    69.                                     rb.velocity = Vector3.zero;
    70.                                     timeTotal = 0;
    71.                                     timeDelay =0;
    72.                              }
    73.                     else if(pathManager.randomDirection == 2)
    74.                              {
    75.                                     rb.AddForce(new Vector3(-4, 5, 4), ForceMode.Impulse);
    76.                                     rb.velocity = Vector3.zero;
    77.                                     timeTotal = 0;
    78.                                     timeDelay =0;
    79.                              }
    80.                     else if(pathManager.randomDirection == 3)
    81.                              {
    82.                                     rb.AddForce(new Vector3(-4, 5, 4), ForceMode.Impulse);
    83.                                     rb.velocity = Vector3.zero;
    84.                                     timeTotal = 0;
    85.                                     timeDelay =0;
    86.                              }
    87.                 }
    88.  
    89.                 else  if (touch.position.x > Screen.width/2 && (timeDelay < 0.5) && (timeTotal == 2))
    90.                 {
    91.                     if(pathManager.randomDirection == 0)
    92.                              {
    93.                                     rb.AddForce(new Vector3(4, 5, 4), ForceMode.Impulse);
    94.                                     rb.velocity = Vector3.zero;
    95.                                     timeTotal = 0;
    96.                                     timeDelay =0;
    97.                              }
    98.                      else if(pathManager.randomDirection == 1)
    99.                              {
    100.                                     rb.AddForce(new Vector3(4, 5, 4), ForceMode.Impulse);
    101.                                     rb.velocity = Vector3.zero;
    102.                                     timeTotal = 0;
    103.                                     timeDelay =0;
    104.                              }
    105.                     else if(pathManager.randomDirection == 2)
    106.                              {
    107.                                     rb.AddForce(new Vector3(4, 5, 4), ForceMode.Impulse);
    108.                                     rb.velocity = Vector3.zero;
    109.                                     timeTotal = 0;
    110.                                     timeDelay =0;
    111.                              }
    112.                     else if(pathManager.randomDirection == 3)
    113.                              {
    114.                                     rb.AddForce(new Vector3(4, 5, 4), ForceMode.Impulse);
    115.                                     rb.velocity = Vector3.zero;
    116.                                     timeTotal = 0;
    117.                                     timeDelay =0;
    118.                              }
    119.                 }
    120.  
    121.                 else  if (touch.position.x > Screen.width/2 && (timeDelay > 0.5) && (timeTotal == 1))
    122.                 {
    123.                     if(pathManager.randomDirection == 0)
    124.                              {
    125.                                     rb.AddForce(new Vector3(4f, 5f, 8f), ForceMode.Impulse);
    126.                                     rb.velocity = Vector3.zero;
    127.                                     timeTotal = 0;
    128.                                     timeDelay =0;
    129.                              }
    130.                      else if(pathManager.randomDirection == 1)
    131.                              {
    132.                                     rb.AddForce(new Vector3(4f, 5f, 8f), ForceMode.Impulse);
    133.                                     rb.velocity = Vector3.zero;
    134.                                     timeTotal = 0;
    135.                                     timeDelay =0;
    136.                              }
    137.                     else if(pathManager.randomDirection == 2)
    138.                              {
    139.                                     rb.AddForce(new Vector3(4f, 5f, 8f), ForceMode.Impulse);
    140.                                     rb.velocity = Vector3.zero;
    141.                                     timeTotal = 0;
    142.                                     timeDelay =0;
    143.                              }
    144.                     else if(pathManager.randomDirection == 3)
    145.                              {
    146.                                     rb.AddForce(new Vector3(4f, 5f, 8f), ForceMode.Impulse);
    147.                                     rb.velocity = Vector3.zero;
    148.                                     timeTotal = 0;
    149.                                     timeDelay =0;
    150.                              }
    151.                 }
    152.  
    153.                 else  if (touch.position.x < Screen.width/2 && (timeDelay) > 0.5 && (timeTotal == 1))
    154.                 {
    155.                     if(pathManager.randomDirection == 0)
    156.                              {
    157.                                     rb.AddForce(new Vector3(-4f, 5f, 8f), ForceMode.Impulse);
    158.                                     rb.velocity = Vector3.zero;
    159.                                     timeTotal = 0;
    160.                                     timeDelay =0;
    161.                              }
    162.                      else if(pathManager.randomDirection == 1)
    163.                              {
    164.                                     rb.AddForce(new Vector3(-4f, 5f, 8f), ForceMode.Impulse);
    165.                                     rb.velocity = Vector3.zero;
    166.                                     timeTotal = 0;
    167.                                     timeDelay =0;
    168.                              }
    169.                     else if(pathManager.randomDirection == 2)
    170.                              {
    171.                                     rb.AddForce(new Vector3(-4f, 5f, 8f), ForceMode.Impulse);
    172.                                     rb.velocity = Vector3.zero;
    173.                                     timeTotal = 0;
    174.                                     timeDelay =0;
    175.                              }
    176.                     else if(pathManager.randomDirection == 3)
    177.                              {
    178.                                     rb.AddForce(new Vector3(-4f, 5f, 8f), ForceMode.Impulse);
    179.                                     rb.velocity = Vector3.zero;
    180.                                     timeTotal = 0;
    181.                                     timeDelay =0;
    182.                              }
    183.                 }
    184.                
    185.                
    186.             }
    187.     }
    188.  
    189.  
    190.  
    191.  
    192.  
    193.        
    194.            
    195.         }
    196.     }
    197.  
    198.  
    199.  
    200.  

    Any help can save me!
    Thanks.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You should mention what is actually happening that is different from what you expected to happen.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    When asking for debugging help, you should always say:
    • What you hoped to accomplish
    • What you did to try to make that happen (code is good, but you should also describe what you were thinking)
    • What actually happened instead
    Formatting your code using standard rules for whitespace would also make it more likely that people will help you.

    I think your basic strategy here was meant to be:
    • If there's 2 touches within a certain time window, trigger the double-touch action
    • If you're past the time window with only 1 touch, trigger the single-touch action.
    What your code actually does is pretty far off from that, though.

    First of all, I don't see any place that you set "timeDelay" to any value other than 0, so you aren't actually measuring the passage of time at all (at least, not in the code you posted).

    Secondly, all of your checks for the amount of time and number of touches are inside the "for each touch" loop, which means you aren't running any of those checks unless the user is currently touching the screen. If the user is not touching the screen when the time window passes, then you have no way of noticing that it's passed.

    Thirdly, you are increasing "timeTotal" for every frame that the user is touching the screen (and for every finger touching in that frame), not for the number of distinct taps. Tapping the screen once could result in a value much higher than 1 if the user doesn't remove their finger quickly enough. (Well, it could if you weren't resetting it to zero after every 2 touches.)
     
    Gdakkutlu and Joe-Censored like this.
  4. Gdakkutlu

    Gdakkutlu

    Joined:
    Nov 9, 2016
    Posts:
    47
    Thanks for answer.
    I've recently checked, and followed your reply.
    I forgot to use timeDelay, so it's not counting anything and that's why it can't recognize double click.
    And after some research, I've wanted to do it with IEnumaretor.
    It works fine now.

    Again thank you for a good answer.