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
hi,
what is your code like so far?
This is how you can get touches:
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):
if (Input.GetMouseButtonDown(0))
{
startTime = Time.time;
}
if (Input.GetMouseButtonUp(0))
{
var timeUsed = Time.time - startTime;
Debug.Log("Time used:" + timeUsed.ToString());
}
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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class JumpCount : MonoBehaviour
{
public float JumpCounter;
float startTime;
PlayerMovement myPlayerMovement;
void Start()
{
myPlayerMovement = GameObject.FindObjectOfType<PlayerMovement>();
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
startTime = Time.time;
}
if (Input.GetMouseButtonUp(0))
{
JumpCounter = Time.time - startTime;
JumpCounter *= 15f;
myPlayerMovement.jumpspeed = JumpCounter;
Debug.Log("JumpCounter " + JumpCounter);
}
}
}
and thats my code in the other object:
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float jumpspeed;
bool canJump;
Rigidbody2D myrb;
void Start()
{
myrb = GetComponent<Rigidbody2D> ();
}
public void Jump() // Die Funktion zum springen
{
if (canJump == true)
{
canJump = false;
myrb.velocity = new Vector2(0f, jumpspeed);
UnityEngine.Debug.Log("Ich kann springen mit " + jumpspeed);
}
UnityEngine.Debug.Log("Ich kann nicht springen mit " + jumpspeed);
}
void OnCollisionEnter2D(Collision2D other) // Die Funktion zum Testen, ob man springen darf
{
canJump = true;
}
}