Search Unity

  1. Get all the Unite Berlin 2018 news on the blog.
    Dismiss Notice
  2. Unity 2018.2 has arrived! Read about it here.
    Dismiss Notice
  3. We're looking for your feedback on the platforms you use and how you use them. Let us know!
    Dismiss Notice
  4. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Learn more.
    Dismiss Notice
  6. ARCore is out of developer preview! Read about it here.
    Dismiss Notice
  7. Magic Leap’s Lumin SDK Technical Preview for Unity lets you get started creating content for Magic Leap One™. Find more information on our blog!
    Dismiss Notice
  8. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Simple game is extremely lagging

Discussion in 'Scripting' started by Rotzak, Aug 13, 2018 at 2:51 AM.

  1. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    56
    Hello,

    I'm working on a car game. A few days ago I noticed the objects that I'm spawning aren't very clear but I didn't bother about it anymore. Today I added alot of UI elements and animations and my game is lagging so hard now, it unplayable really... So I've spent the last few hours with disabling all these objects and animations and removing alot of stuff of the update function but still the game is lagging extremely.

    Here is a little video of the lagg, look at the car with number 87 you can barely read it:


    Here are the stats. Isn't it alot of fps for such a small game?


    And here is a picture of the profiler. To be honest I don't understand most of these parameters.


    peak:




    PS:
    - I'm using instantiate to spawn, but there are max 4-5 clones at a time.
    - Game is lagging on phone as an apk build BUT plays fine (still a bit laggy) on the computer!
     
    Last edited: Aug 13, 2018 at 2:56 AM
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    1,571
    Gfx.WaitForPresent means that Unity has done all of its processing and is sitting around waiting for the GPU draw the current game's frame, which is basically an ideal state to be in.

    1.3ms CPU time is extremely good.

    Your game doesn't seem laggy so much as you're relying on physics without interpolation, and is thus only moving the objects as often as your fixed timestep. Try turning Interpolation on, in the Rigidbody components for your cars, and the visuals should feel smoother.
     
    Rotzak likes this.
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    15,371
    Looking closely, it actually looks like a camera problem. Is your camera moving? That's where I would look first.

    If not I would take a close look at the code that is moving the road.
     
    Rotzak likes this.
  4. Peter77

    Peter77

    Joined:
    Jun 12, 2013
    Posts:
    2,437
    Do not profile your game running in the editor, it yields incorrect results, profile a build running on the target hardware instead.

    The following video goes into details why profiling in the editor isn't a great idea.
     
    Rotzak likes this.
  5. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    56
    Thanks for the answer. I've turned on interpolation for all the cars but unfortunately it didn't change anything. Here is a picture of my rigidbody 2D settings now:
     
  6. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    56
    Thanks for the answer. Idd it looks like the camera is making small movements. I've checked it and the camera has no script attached so can't move probably. Here is a picture of the camera settings, nothing wrong with them I think:


    Can't see something wrong in the script of the track move. Also I turned the background off and builded on my phone but it's still lagging.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class trackMove : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     Vector2 offset;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.  
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.             offset = new Vector2(0, Time.time * speed);
    18.  
    19.             GetComponent<Renderer>().material.mainTextureOffset = offset;
    20.     }
    21. }
    22.  
     
  7. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    56
    Also I want to notice the lagg starts from the beginning so even when the game is paused. I don't know if you can see it in this video but in my opinion the ghost that moves up after countdown is really stuttering (animation). Here is a video:



    When the game became this slow there also happened something in the menu. In the menu you can choose out of 8 players. 6 out of the 8 player sprites where you can choose from were heavily blurred. Notice that these are just sprites! I fixed this by changing the max size of the sprites from 2048 to 1024pixels .
     
    Last edited: Aug 13, 2018 at 9:57 AM
  8. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    56
    I've disabled all objects, audio and other stuff and here is the result on my phone (apk build):



    Doesn't look smooth at all to me lol. Any ideas really don't know what to do now :eek:
     
  9. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    226
    What version of Unity are you using?

    Maybe use renderer.sharedmaterial instead of material since the material property of renderer is creating a new instance everytime you call it. Since you’re doing it every frame you’re creating a material every frame.

    Read the note & code, you should call it once and store it. Then use that instanced reference.
    https://docs.unity3d.com/ScriptReference/Renderer-material.html

    What is the target frame rate set to? Is it 30 or 60? (debug it on the device, not in unity editor)
    Debug.Log(Application.targetFrameRate);

    if 30 try setting it to 60 (note that this does use more battery)
    Application.targetFrameRate = 60;


    Have you tried a development build on the phone and profile that, instead of the Editor?

    Is the spike also occuring if you lower the speed variable? Is it less frequent when you lower it?
     
    Last edited: Aug 13, 2018 at 4:09 PM
    Rotzak and FernandoHC like this.
  10. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    56
    Thanks for the answer will try this out tommorow. I was using 2018.1 and I've updated to 2018.2 just now, so many TextMesh errors...
     
  11. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    239
    Listen man, i have the SAME problem as yours!!!
    I've opened a thread few days ago and no one could help me.
    I consider remove my project now because this stupid thing...
    It's just not smooth and there is no excuse!
    Profiler says nothing, script is optimized i guess and the sprites are low compressed.
    If you find an answer pleaseee send me a message.
     
  12. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    56
    Sure will do what kind of game was it? Were you also spawning objects top down?
     
  13. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    56
    Update:
    I've made a new scene in my current project with only a moving track. I disabled all the other scenes in the build settings and builded it to my phone. Here is the result:




    After I made a new project with exact the same objects:



    Result: It's sad I cant share what I see but the new project isn't stuttering at all. The scene made in the current project is stuttering heavily. It's not really stuttering but you can see that the lines are vibrating.

    Conclusion: I really don't have an idea but this is really weird isn't it exact the same becuase I disabled all the other scenes in the current project??? I mean in both cases there is just one script running if I'm correct.
     
    Last edited: Aug 14, 2018 at 12:23 AM
  14. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    56
    So first I added
    Debug.Log(Application.targetFrameRate);
    in the Update function of a random script. The console was constantly debugging "-1". After I added in the start function of the same script:
    Application.targetFrameRate = 30;
    . Builded in to my phone and the vibrating was even worse. After I did another build but now I wrote this in the start function:
    Application.targetFrameRate = 60;
    .

    Result: It's much better now but it's still not OK. It looks like the vibrations are in the car itself now and not around it anymore.

    What can I conclude out of this? And what can I do to remove these vibrations totally?
    Also I have the feeling the vibrations are getting worse the longer I play. Not sure of this it could be also eye deception.
     
    Last edited: Aug 14, 2018 at 12:55 AM
  15. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    1,571
    -1 just means there's no limit and it'll go fast as it can.

    You should post some code.
     
  16. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    226
  17. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    239
    Endless runner, I used object pooling.
     
  18. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    56
  19. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    56
    It could be the code, i'm an beginner. I've started c# and Unity on 17 June and I never did anything with programming in my life. But what code would be relevant? I Have around 20-30 scripts and I'm calling probably way too much in the update functions.

    Here is my enemy car mover script. The enemy cars are really unclear and vibrating. So that's actually the biggest problem atm. The player car on the other hand is clear because it isn't moving.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyCarMove : MonoBehaviour {
    6.  
    7.     public float speed = 8.0f;
    8.  
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.    
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.  
    18.         transform.Translate(new Vector3(0, 1, 0) * speed * Time.deltaTime);
    19.  
    20.     }
    21. }
    22.  
    Here is my enemycarspawner script this is probably a bit vague and not optimized. The script is attached to an empty gameobject:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.Linq;
    5.  
    6.  
    7. public class CarSpawner : MonoBehaviour {
    8.  
    9.     public GameObject[] cars; //Change to array to add more different cars (datatype = gameObject ,en [] verwijst naar een array, en cars is de naam van de array)
    10.     //int[] numbers = { 0, 1, 2, 3, 4, 5, 6, 7 };
    11.  
    12.     int carNo;
    13.     int carNoTwo;
    14.     int randomSpawnNo;
    15.  
    16.     public float maxPos = 1.60f;
    17.  
    18.     bool SpawnTwoCars;
    19.     float dist;
    20.  
    21.     public float delayCarTimer;
    22.     float carTimer;
    23.     //public float truckTimer = 5.0f;
    24.  
    25.     public List<int> xPositions = new List<int>();
    26.  
    27.     float notSpawnPositionMin;
    28.     float notSpawnPositionMax;
    29.  
    30.     float newMinPosition;
    31.     float newMaxPosition;
    32.  
    33.     bool spawnOnLeftSide;
    34.     bool spawnOnRightSide;
    35.  
    36.     //bool oneTime = true;
    37.  
    38.     // Use this for initialization
    39.     void Start() {
    40.  
    41.         carTimer = delayCarTimer; //Timer is bij start gelijk aan één
    42.                                   //truckTimer = delayTruckTimer;
    43.  
    44.         Vector3 StartCarPos = new Vector3(0, transform.position.y, transform.position.z); //Positie van het spawnen
    45.  
    46.         carNo = Random.Range(0, 77);
    47.  
    48.         Instantiate(cars[carNo], StartCarPos, transform.rotation); //We use the position and rotation of the CarSpawnPosition (empty gameobject)
    49.  
    50.     }
    51.  
    52.     // Update is called once per frame
    53.     void Update()
    54.     {
    55.         if (UImanager.score <= 5)
    56.         {
    57.             delayCarTimer = Random.Range(0.60f, 0.90f);
    58.         }
    59.         if (UImanager.score > 5)
    60.         {
    61.             delayCarTimer = Random.Range(0.60f, 0.85f);
    62.         }
    63.         if (UImanager.score > 10)
    64.         {
    65.             delayCarTimer = Random.Range(0.60f, 0.80f);
    66.         }
    67.  
    68.         if (UImanager.score > 15)
    69.         {
    70.             delayCarTimer = Random.Range(0.55f, 0.70f);
    71.         }
    72.  
    73.         /*
    74.         if (UImanager.score > 40)
    75.         {
    76.             delayCarTimer = 0.60f;
    77.         }
    78.         if (UImanager.score > 50)
    79.         {
    80.             delayCarTimer = 0.60f;
    81.         }
    82.         if (UImanager.score > 100)
    83.         {
    84.             delayCarTimer = 0.60f;
    85.         }
    86.         if (UImanager.score > 200)
    87.         {
    88.             delayCarTimer = 0.65f;
    89.         }
    90.         if (UImanager.score > 500)
    91.         {
    92.             delayCarTimer = 0.60f;
    93.         }
    94.         if (UImanager.score > 700)
    95.         {
    96.             delayCarTimer = 0.60f;
    97.         }
    98.         if (UImanager.score > 800)
    99.         {
    100.             delayCarTimer = 0.60f;
    101.         }
    102.         if (UImanager.score > 900)
    103.         {
    104.             delayCarTimer = 0.55f;
    105.         }
    106.         if (UImanager.score > 950)
    107.         {
    108.             delayCarTimer = 0.50f;
    109.         }
    110.         */
    111.  
    112.         carTimer -= Time.deltaTime; //De echte tijd wordt afgetrokken van de timervalue die bij start één is, en wordt daaraan gelijkgesteld
    113.                                     //truckTimer -= Time.deltaTime;
    114.  
    115.         SpawnOneCar();
    116.  
    117.         SpawnTwoCarsAtSameTime();
    118.  
    119.         /*if (truckTimer <= 0)
    120.         {
    121.             SpawnTruck();
    122.         }*/
    123.     }
    124.  
    125.     public void SpawnOneCar()
    126.     {
    127.         if (carTimer <= 0 && SpawnTwoCars == false) //Zodra het spel één seconde bezig is zal er aan deze voorwaarde voldaan zijn
    128.         {
    129.                 //Get last position of the list
    130.                 int number = xPositions.Count;
    131.                 int lastItem = xPositions[number - 1];
    132.  
    133.                 if (lastItem <= 0)
    134.                 {
    135.                     spawnOnRightSide = true;
    136.                 }
    137.  
    138.                 if (lastItem > 0)
    139.                 {
    140.                     spawnOnLeftSide = true;
    141.                 }
    142.  
    143.                 //Not spawning between a range of +-1 of the last car position
    144.                 notSpawnPositionMin = (lastItem) - 1f;
    145.                 notSpawnPositionMax = (lastItem) + 1f;
    146.  
    147.                 //Spawn positions for the next car
    148.                 newMinPosition = Random.Range(-maxPos, notSpawnPositionMin);
    149.                 newMaxPosition = Random.Range(notSpawnPositionMax, maxPos);
    150.  
    151.                 //Round to int so we can add the new car position to the list
    152.                 int checkXAgainRight = Mathf.RoundToInt(newMaxPosition);
    153.                 int checkXAgainLeft = Mathf.RoundToInt(newMinPosition);
    154.  
    155.                 if (spawnOnRightSide == true)
    156.                 {
    157.                     xPositions.Add(checkXAgainRight);
    158.                 }
    159.  
    160.                 if (spawnOnLeftSide == true)
    161.                 {
    162.                     xPositions.Add(checkXAgainLeft);
    163.                 }
    164.  
    165.             SpawnOneCarLeftOrRight();
    166.         }
    167.     }
    168.  
    169.     public void SpawnOneCarLeftOrRight()
    170.     {
    171.             if (spawnOnLeftSide == true)
    172.             {
    173.                 Vector3 NewCarPos = new Vector3(newMinPosition, transform.position.y, transform.position.z); //Positie van het spawnen
    174.  
    175.                 carNo = Random.Range(0, 77);
    176.  
    177.                 Instantiate(cars[carNo], NewCarPos, transform.rotation); //We use the position and rotation of the CarSpawnPosition (empty gameobject)
    178.  
    179.                 carTimer = delayCarTimer; //We zetten de timer terug naar zijn startvalue, welke een is
    180.  
    181.             randomSpawnNo = Random.Range(0, 8);
    182.  
    183.                 spawnOnLeftSide = false;
    184.  
    185.                 if (randomSpawnNo == 5)
    186.                 {
    187.                     SpawnTwoCars = true;
    188.                 }
    189.             }
    190.  
    191.             if (spawnOnRightSide == true)
    192.             {
    193.                 Vector3 NewCarPos = new Vector3(newMaxPosition, transform.position.y, transform.position.z); //Positie van het spawnen
    194.  
    195.                 carNo = Random.Range(0, 77);
    196.  
    197.                 Instantiate(cars[carNo], NewCarPos, transform.rotation); //We use the position and rotation of the CarSpawnPosition (empty gameobject)
    198.  
    199.                 carTimer = delayCarTimer; //We zetten de timer terug naar zijn startvalue, welke een is
    200.  
    201.             randomSpawnNo = Random.Range(0, 8);
    202.  
    203.                 spawnOnRightSide = false;
    204.  
    205.                 if (randomSpawnNo == 5)
    206.                 {
    207.                     SpawnTwoCars = true;
    208.                 }
    209.             }
    210.     }
    211.  
    212.     public void SpawnTwoCarsAtSameTime()
    213.     {
    214.             Vector3 carPosOne = new Vector3(Random.Range(0.70f, maxPos), transform.position.y, transform.position.z);
    215.  
    216.             float secondMin = -maxPos;
    217.             float secondMax = maxPos;
    218.             float halfway = 0;
    219.  
    220.             if (carPosOne.x >= halfway)
    221.             {
    222.                 secondMax = carPosOne.x - 2.30f;
    223.             }
    224.  
    225.             else
    226.             {
    227.                 secondMin = carPosOne.x + 2.30f;
    228.             }
    229.  
    230.             Vector3 carPosTwo = new Vector3(Random.Range(secondMin, secondMax), transform.position.y, transform.position.z);
    231.  
    232.             //dist = Vector3.Distance(carPosOne, carPosTwo);
    233.  
    234.             carNo = Random.Range(0, 77);
    235.             carNoTwo = Random.Range(0, 7);
    236.  
    237.             if (SpawnTwoCars == true && carTimer <= 0)
    238.             {
    239.                 Instantiate(cars[carNo], carPosOne, transform.rotation); //We use the position and rotation of the CarSpawnPosition (empty gameobject)
    240.  
    241.                 Instantiate(cars[carNoTwo], carPosTwo, transform.rotation);
    242.  
    243.                 carTimer = delayCarTimer; //We zetten de timer terug naar zijn startvalue, welke een is
    244.  
    245.                 SpawnTwoCars = false;
    246.             }
    247.     }
    248.  
    249.     /*public void SpawnTruck()
    250.     {
    251.         if (oneTime == true)
    252.         {
    253.         Vector3 TruckPos = new Vector3(Random.Range(-maxPos, maxPos), transform.position.y, transform.position.z); //Positie van het spawnen
    254.  
    255.         Instantiate(truck, TruckPos, transform.rotation); //We use the position and rotation of the CarSpawnPosition (empty gameobject)
    256.  
    257.         carTimer = delayCarTimer; //We zetten de timer terug naar zijn startvalue, welke een is
    258.  
    259.         truckTimer = 5.0f;
    260.  
    261.         oneTime = false;
    262.  
    263.         }
    264.    
    265.     }*/
    266.  
    267. }
    268.  
    269.  
    270.  
    And here is my enemycar import settings. I've tried a few different settings but maybe you spot something wrong:

     
    Last edited: Aug 14, 2018 at 1:14 PM
  20. Rotzak

    Rotzak

    Joined:
    Jul 11, 2018
    Posts:
    56
  21. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    226
    Unity remote is just casting the screen onto your device and use the device input to hand it back to the editor.
    Personally I never use the Unity Remote because it just buggy and sucks. It isn't the same experience as building the APK for the target device.

    As Shayke said in an endless runner it is highly advisable to use object pooling.
    You don't want to constantly keep instantiating prefabs again and again.
    Rather on start, instantiate a few to fill the pool a bit by an initial amount.
    While running -> get a car from the pool (set active) -> track its life cycle (position) -> if end of life cycle (out of screen bounds) -> return to the pool (set inactive)
    if there is no car available from the pool instantiate one.
    Something along those lines.

    You're currently moving the texture using a textureoffset, not sure how precise that is and whether it has an effect when you increase the texture offset by such a high amount (since it builds up every frame).

    If I were making an endless runner like that I'd move 2 or 3 road pieces. If it reaches a certain Y point, reset it back to the top.
     
    Last edited: Aug 15, 2018 at 1:20 AM
  22. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    15,371
    Its great for working out input schemes that can't be reproduced on the computer. Things like cameras or gyro data. But otherwise I agree with you.