Search Unity

Question How to reduce time spent in Update and GC Unity For Android Game

Discussion in 'Scripting' started by naptechgames, Sep 6, 2021.

  1. naptechgames

    naptechgames

    Joined:
    Jun 7, 2020
    Posts:
    4
    When I build Samsung M21 & Samsung M20 My game was lag, Display Graphics Buffer Problem.
    Please Suggest me what is wrong about it.
    WhatsApp Image 2021-09-06 at 6.01.43 PM.jpeg Screenshot_2.png Screenshot_4.png Screenshot_5.png Screenshot_6.png WhatsApp Image 2021-09-06 at 6.01.43 PM.jpeg Screenshot_2.png Screenshot_4.png Screenshot_5.png Screenshot_6.png Screenshot_2.png WhatsApp Image 2021-09-06 at 6.01.43 PM.jpeg Screenshot_2.png Screenshot_4.png Screenshot_5.png Screenshot_6.png WhatsApp Image 2021-09-06 at 6.01.43 PM.jpeg Screenshot_2.png Screenshot_4.png Screenshot_5.png Screenshot_6.png

    using System.Collections;
    using System.Collections.Generic;
    using PathCreation;
    using UnityEngine;

    public class Player : MonoBehaviour
    {
    public static Player Instance;

    // Use Path Creator Tools
    private PathCreator pathCreator;

    // enum State working on When game is Start, Dead And Complete
    private enum State { Menu, Game, Dead, Complete }
    private State state = State.Menu;

    [SerializeField]
    private float maxSpeed = 5; // Player Maximum Speed

    [SerializeField]
    private float accelTime = 0.5f; // Player Controll Speed

    [SerializeField]
    private Trail trail = null;

    private bool speedUp;
    private float speed;
    private float distanceTravelled;
    private float trailDist;
    private float totalDistance;

    public void Update()
    {

    if (Input.GetMouseButtonDown(0))
    {
    speedUp = true;
    }

    if (Input.GetMouseButtonUp(0))
    {
    speedUp = false;
    }

    // Controll Player Speed
    float accel = Time.deltaTime * maxSpeed / accelTime;
    speed = Mathf.Clamp(speedUp ? speed + accel : speed - accel, 0, maxSpeed);


    // This Logic Work When Game is Start
    if (state == State.Game)
    {
    distanceTravelled += speed * Time.deltaTime;
    SetPosition();
    UIController.Instance.UpdateLevelProgress(distanceTravelled / totalDistance); // See Player Position In UI Progress_Bar

    if (distanceTravelled - trailDist > 0.5f)
    {
    trailDist += 0.5f;
    trail.AddCubePoint(transform.position, transform.forward);
    }

    if (distanceTravelled >= totalDistance) // Game State Complete
    {
    state = State.Complete;
    Game.Instance.CompleteLevel();
    }
    }
    }

    // Set Player Starting Position When Game is Start
    private void SetPosition()
    {
    transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled, EndOfPathInstruction.Stop);
    Vector3 fwd = pathCreator.path.GetRotationAtDistance(distanceTravelled, EndOfPathInstruction.Stop) * Vector3.forward;
    transform.forward = fwd;
    } WhatsApp Image 2021-09-06 at 6.01.43 PM.jpeg
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,407
    do you have Profiler screenshot?
     
  3. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    353
    Give us screenshot of your profiler. I recommend using deep profile with callstack so you can see what exactly generates garbage.
     
  4. naptechgames

    naptechgames

    Joined:
    Jun 7, 2020
    Posts:
    4
    Unity Version 2020.3.17f1
     

    Attached Files:

    • 1.png
      1.png
      File size:
      144.9 KB
      Views:
      261
    • 2.png
      2.png
      File size:
      160.3 KB
      Views:
      262
    • 3.png
      3.png
      File size:
      165.1 KB
      Views:
      263
    • 4.png
      4.png
      File size:
      153.3 KB
      Views:
      268
    • 5.png
      5.png
      File size:
      165.8 KB
      Views:
      259
  5. naptechgames

    naptechgames

    Joined:
    Jun 7, 2020
    Posts:
    4
    Unity Version 2020.3.17f1
     

    Attached Files:

    • 6.png
      6.png
      File size:
      149.5 KB
      Views:
      255
    • 7.png
      7.png
      File size:
      153.8 KB
      Views:
      255
    • 8.png
      8.png
      File size:
      155.5 KB
      Views:
      255
  6. VolodymyrBS

    VolodymyrBS

    Joined:
    May 15, 2019
    Posts:
    150
    Looks like
    MathUtility.LockTransformToSpace
    produce a lot of garbage (100B for one call).
    I recommend to start investigation from that method.
    Could you share that method code?
     
    Last edited: Sep 8, 2021
  7. naptechgames

    naptechgames

    Joined:
    Jun 7, 2020
    Posts:
    4
    [SerializeField, HideInInspector]
    PathSpace space;
    public PathSpace Space {
    get {
    return space;
    }
    set {
    if (value != space) {
    PathSpace previousSpace = space;
    space = value;
    UpdateToNewPathSpace (previousSpace);
    }
    }
    } 10.PNG