1
\$\begingroup\$

I trying to make an object jump from 1 point to another point. How far it jumps depends on how long the player holds the touch anywhere on the screen. It jumps when player releases his touch.

At the code below,I able to detect the moment player release his finger.

if (Input.touchCount > 0) {
    Touch touch = Input.GetTouch(0);

    // Handle finger movements based on touch phase.
    switch (touch.phase) {
        // Finger start touching the screen
        case TouchPhase.Began:
            print("Start tapping");
            break;

        // Finger leaving the screen
        case TouchPhase.Ended:
            //when finger release, the object jump
            float rightForce = Time.deltaTime * 10000 / 2;
            float jumpForce = Time.deltaTime * 30000 / 2;
            box2D.AddForce(new Vector2(rightForce, jumpForce), ForceMode2D.Force);
            break;
    }
}

As you can see I able to get the Time.deltaTime which is I assume is how long the player hold the screen.

What I want to do is calculate the rightForce and jumpForce added to the object depending on the Time user hold the touch.

float rightForce = Time.deltaTime * 10000 / 2;
float jumpForce = Time.deltaTime * 30000 / 2;

Because if I use the 2 line of code above, the result will be almost the same everytime.

So what is the correct way to determine the rightForce and jumpForce depending the time user hold touch?

\$\endgroup\$
  • 2
    \$\begingroup\$ "As you can see I able to get the Time.deltaTime which is I assume is how long the player hold the screen" — there's no need to assume anything when you can just read the documentation, which will tell you plainly that this is NOT what deltaTime measures. \$\endgroup\$ – DMGregory Sep 24 '19 at 10:17
  • 1
    \$\begingroup\$ @DMGregory sir can u just point me a 1 single keyword about how to do this? i not asking for code,but only 1 single keyword to point me in a right direction.totally beginner here..tq in advance \$\endgroup\$ – ken Sep 24 '19 at 12:02
  • 2
    \$\begingroup\$ You already asked for help above — you do not need to repeat the request in the comments. When someone has an answer for you, they'll post it in the answers section below. \$\endgroup\$ – DMGregory Sep 24 '19 at 12:05
3
\$\begingroup\$

As you can see I able to get the Time.deltaTime which is I assume is how long the player hold the screen.

Your assumption is wrong. Time.deltaTime gives you the time elapsed between the last frame and current frame.

Your question boils down to "How do I measure touch time across multiple frames?". And the answer is simple: You accumulate the delta time between frames.

public float touchTime;

private void Update() {
    if (Input.touchCount < 1)
        return;

    var touch = Input.GetTouch(0);

    switch (touch.phase) {
        case TouchPhase.Began:
            touchTime = 0;
            break;
        case TouchPhase.Ended:
            Debug.Log($"The player touched the screen for {touchTime} seconds.");
            break;
        default:
            touchTime += Time.deltaTime; //<<<<<The bit you are misunderstanding and missing
            break;
    }
}

This should be enough for you to understand what's wrong and how to fix it. Learning from the code above to make your own code work is left as an exercise for you.

Note that you can simplify this by making the system based on timestamping rather than accumulation. For that, Time.time gives you the time in seconds since the start of the game. But again, that is left as an exercise for you.

Have fun.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.