Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Touch detected too late on mobile device

Discussion in 'Scripting' started by mondeon, Nov 10, 2015.

  1. mondeon

    mondeon

    Joined:
    May 29, 2015
    Posts:
    46
    Hello Unity Community!

    for my 2d game (made with Unity 5.1) when the player touches the screen anywhere (press and hold => Input.touchCount > 0) a force is being applied continuously on the character, until the player releases(Input.touchCount == 0). Simplified, the code is like:

    private bool isPressed;

    void Update(){
    if (Input.touchCount > 0)
    isPressed = true;
    else
    isPressed = false;
    }

    void FixedUpdate(){
    if (isPressed)
    rb.AddForce(transform.forward * thrust);
    else
    rb.velocity = 0;
    }

    The problem is: the touch events are detected on my android phone ~0.3s later, which is kind of critical for the game. The setting of velocity and adding force is a bit more complicated than stated, but the principle is the same. On my 7 years old crappy thinkpad everything is working perfect, no delay at all. I tested with Input.GetMouseButtonDown(0), but the result was the same. Am I missing something? Should the touch detection be implemented in the Update() method and changes in physics in FixedUpdate()? Is that touch detection delay of 0.3s normal for Unity games? Thanks for any advice!
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    FixedUpdate isn't called every frame like Update. Mybe you could take rb.velocity = 0 one line below you set isPressed = false;
     
  3. mondeon

    mondeon

    Joined:
    May 29, 2015
    Posts:
    46
    That is weird. Looks like the difference in time between Update() and FixedUpdate() calls can be quite dramaticI I moved all the code in the Update() method and now it works much more fluent on the phone. Though on my old laptop with HD 3000 integrated graphics it's a bit choppy now..

    My question now is: does it make sense to detect the screen touch in FixedUpdate()?