Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question Why is 3D mobile game lagging after touching the screen?

Discussion in 'Input System' started by mikolajmichalczak, May 27, 2023.

  1. mikolajmichalczak

    mikolajmichalczak

    Joined:
    Mar 3, 2023
    Posts:
    1
    Hi, I am creating a 3D mobile game in Unity where a ball rolls, and the user stops moving platforms by touching the screen.
    The problem is that the game lags for a while after about 0.5 seconds every time the screen is touched, but it runs smoothly without any touch input.
    Initially, I thought of checking the methods related to touch input, but even after commenting them out, the problem persists. I am wondering how it is possible for the game to still lag after a touch when there is no relevant code related to it.
    The game is relatively simple at the moment, without any textures. The main logic involves moving the ball, camera, and platforms, spawning platforms in front of the ball, and destroying objects behind it using Prefabs.

    Some technical details:
    • I am using Xiaomi Mi 10T for testing.

    • The lag is slightly reduced when manually setting the target frame rate to 60 using Application.targetFrameRate = 60;.

    • I have performed some optimization for mobile devices.

    • Here is the function for moving the ball:
    Code (CSharp):
    1.  
    2. void FixedUpdate() {
    3.    if (!isBallStopped)
    4.    {
    5.       var forwardMove = transform.forward * (speed * Time.fixedDeltaTime);
    6.       rb.MovePosition(rb.position + forwardMove);
    7.    }
    8. }
    • Here is the function for moving platforms:
    Code (CSharp):
    1.  
    2. void FixedUpdate() {
    3.    if (!platformTile.isMoving) return;
    4.    if (Vector3.Distance(transform.position, waypoints[currentWaypointIndex].transform.position) < .1f)
    5.    {      
    6.       currentWaypointIndex++;
    7.       if (currentWaypointIndex >= waypoints.Length) {
    8.          currentWaypointIndex = 0;
    9.       }
    10.    }
    11.    transform.position = Vector3.MoveTowards(transform.position, waypoints[currentWaypointIndex].transform.position, speed * Time.fixedDeltaTime);
    12. }
    13.