Search Unity

Touch Duration

Discussion in 'Scripting' started by Schoeneberg, Aug 2, 2020.

  1. Schoeneberg

    Schoeneberg

    Joined:
    Jul 28, 2020
    Posts:
    11
    Hello guys,
    I need help I try since more than 4 days to safe the duration of a touch on my button but I dont got it. Maybe you can help me and tell me how I can safe the duration of a touch at a button in a variable. Thanks
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    hi,

    what is your code like so far?

    This is how you can get touches:
    https://docs.unity3d.com/ScriptReference/Input.GetTouch.html

    Then it is pretty much the same as with mouse/key press, just check how long button/touch was held (note, the example is using mouse button, but you get the idea):
    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0))
    2. {
    3.     startTime = Time.time;
    4. }
    5.  
    6. if (Input.GetMouseButtonUp(0))
    7. {
    8.     var timeUsed = Time.time - startTime;
    9.     Debug.Log("Time used:" + timeUsed.ToString());
    10. }
     
  3. Schoeneberg

    Schoeneberg

    Joined:
    Jul 28, 2020
    Posts:
    11
    Thanks for the help, now it is easy to change this code into the same with touch. Here is my code which you wanted to see. I want to change the value jumpspeed into the same like JumpCounter. Thats my code in the first object:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class JumpCount : MonoBehaviour
    7. {
    8.     public float JumpCounter;
    9.     float startTime;
    10.     PlayerMovement myPlayerMovement;
    11.  
    12.     void Start()
    13.     {
    14.         myPlayerMovement = GameObject.FindObjectOfType<PlayerMovement>();
    15.     }
    16.  
    17.     void Update()
    18.     {
    19.         if (Input.GetMouseButtonDown(0))
    20.         {
    21.             startTime = Time.time;
    22.         }
    23.         if (Input.GetMouseButtonUp(0))
    24.         {
    25.             JumpCounter = Time.time - startTime;
    26.             JumpCounter *= 15f;
    27.             myPlayerMovement.jumpspeed = JumpCounter;
    28.             Debug.Log("JumpCounter " + JumpCounter);
    29.         }
    30.     }
    31. }
    and thats my code in the other object:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Collections.Specialized;
    4. using System.Diagnostics;
    5. using UnityEngine;
    6.  
    7. public class PlayerMovement : MonoBehaviour
    8. {
    9.     public float jumpspeed;
    10.     bool canJump;
    11.     Rigidbody2D myrb;
    12.  
    13.     void Start()
    14.     {
    15.         myrb = GetComponent<Rigidbody2D> ();
    16.     }
    17.  
    18.     public void Jump()                                                         // Die Funktion zum springen
    19.     {
    20.         if (canJump == true)
    21.         {
    22.             canJump = false;
    23.             myrb.velocity = new Vector2(0f, jumpspeed);
    24.             UnityEngine.Debug.Log("Ich kann springen mit " + jumpspeed);
    25.         }
    26.         UnityEngine.Debug.Log("Ich kann nicht springen mit " + jumpspeed);
    27.     }
    28.     void OnCollisionEnter2D(Collision2D other)                                 // Die Funktion zum Testen, ob man springen darf
    29.     {
    30.         canJump = true;
    31.     }
    32. }
     
    Last edited: Aug 3, 2020