playMaker

Author Topic: Lag when activating game object  (Read 5381 times)

DennisJensen

  • Junior Playmaker
  • **
  • Posts: 85
    • View Profile
Lag when activating game object
« on: July 06, 2014, 04:25:36 AM »
Hi Playmaker,

This might not be a playmaker question in general, but this community is so great, I thouht I would ask it here. I posted a problem with optimization some time ago  on the forum which I have been working on since then. Link to old thread.

Game:
It's a endless runner, in 2d, with a lot of stuff comming towards you. See attached screenshot.

The problem:
The first game you play lags. Not horrible, but a little bit all the time.
The game lags a lot on Iphone 4. On Iphone 5 it runs smooth after the first game.

In the profile thing, I can see that activate game object takes a lof of power in the beginning

Setup:
I use three different pools for, Ammo, Enemies, Props (Coins etc). The environment is using eight different platforms, where two are shown at the same time, I activate it when I need to use a platform. There is three background planes and four midtground.

I read that unity takes more effort when it shows a game object for the first time. So maybe thats what causing it?

I use 3d physics, these take more effort than 2d physics, I only used them because this is new to me, and I didn't know better. I could change it to 2d but it would take a while, and if it didn't help it would be waste of time.

I use a lot of trigger events, and I use some global events. How bad are these? I have 2 environment modules showing at once each with about 20-30 placeholder (Just a box with a trigger) objects waiting to be triggered. I use to have a lot of every frame checks but I got rid of most of them.

We are going to publish this game soon, so any inputs are very welcome.

Thank you.

/Dennis
Game Designer, CEO, Artist
http://2ndStudio.com

escpodgames

  • Hero Member
  • *****
  • Posts: 673
    • View Profile
    • Assets
Re: Lag when activating game object
« Reply #1 on: July 07, 2014, 01:34:43 AM »
Ran into this exact issue on FactoryBird (didn't fix it) and would love to know how to get around it as releasing a laggy game gets hit hard in ratings.

escpodgames

  • Hero Member
  • *****
  • Posts: 673
    • View Profile
    • Assets
Re: Lag when activating game object
« Reply #2 on: July 08, 2014, 02:42:30 AM »
This is all I can find on the issue :S

"A lot of Unity objects are lazy-loaded so their information will be loaded into memory the first time you use them and that may cause your game to lag. Textures are one of the biggest offender, but their are a few others.

The solution is to force Unity to load your objects at the beginning of the scene by instantiating all your prefabs, or assigning a material with the texture someone in the scene so it will be rendered once."

http://answers.unity3d.com/questions/57538/why-does-my-game-lag-the-first-time-this-prefab-is.html

DennisJensen

  • Junior Playmaker
  • **
  • Posts: 85
    • View Profile
Re: Lag when activating game object
« Reply #3 on: July 09, 2014, 01:33:00 AM »
Hmm okay, it's interesting.

It would be good to find a solution also for future projects.
Game Designer, CEO, Artist
http://2ndStudio.com

escpodgames

  • Hero Member
  • *****
  • Posts: 673
    • View Profile
    • Assets
« Last Edit: July 09, 2014, 09:26:42 PM by LampRabbit »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12417
  • Official Playmaker Support
    • View Profile
Re: Lag when activating game object
« Reply #5 on: July 10, 2014, 07:15:29 AM »
Hi,

 you need a pool manager system to solve this elegantly. typically like this one:

https://hutonggames.fogbugz.com/default.asp?W839


 Bye,

 Jean

DennisJensen

  • Junior Playmaker
  • **
  • Posts: 85
    • View Profile
Re: Lag when activating game object
« Reply #6 on: July 14, 2014, 10:42:38 AM »
LampRabbit:
Interesting solution, we will check it out, and see if it does the trick.

Jean Fabre:
I honestly don't think it can be solved with a pool manager, we are already using a pool manager for all our objects except the environment which reuses the objects. But I'm new to unity so I might be wrong.
Game Designer, CEO, Artist
http://2ndStudio.com

escpodgames

  • Hero Member
  • *****
  • Posts: 673
    • View Profile
    • Assets
Re: Lag when activating game object
« Reply #7 on: July 14, 2014, 03:45:04 PM »
Another thing I was able to do to smooth over the lag was to enable gameobjects over a few frames rather all at once.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12417
  • Official Playmaker Support
    • View Profile
Re: Lag when activating game object
« Reply #8 on: July 15, 2014, 12:52:09 AM »
Hi,

 yes, I see, it's not the fact that you activate a component but rather what it does when you activate it that takes time. What are these components? and indeed is it a big mesh?

Bye,

 Jean

DennisJensen

  • Junior Playmaker
  • **
  • Posts: 85
    • View Profile
Re: Lag when activating game object
« Reply #9 on: July 15, 2014, 06:31:08 AM »
The suggestion from above link didn't work  :'(

Thank you for trying to help. If you have a iOS device I can also put you on the test list so you can test it via testflight.

Jean Fabre:
It's 152 tris (76 polygons) for once piece of environment, however I use a mesh colider for each as well, so I guess its about the doubble? The screen shows one and sometimes two pieces at the same time.

Each environment piece has a FSM that check the position of the piece each second, if the piece if off screen it deactivates the piece. Ontop of all pieces I translate the environment in one big group on every frame. I tried reducing these FSM's.

I do use a lot of box coliders as placeholders for where the props goes and enemies, once the box trigger is on screen it spawns a enemy or prop in a pool. One environment piece has about 20 box coliders.

LampRabbit:
I tried showing both the environment and the props in the main menu to reduce loading but it didn't work.
Game Designer, CEO, Artist
http://2ndStudio.com

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12417
  • Official Playmaker Support
    • View Profile
Re: Lag when activating game object
« Reply #10 on: July 16, 2014, 04:37:56 AM »
Hi,

 are they flagged "static" or are you moving them somehow? Physics setup can have many flaws on that regards with high performances cost if not careful.

Bye,

 Jean

DennisJensen

  • Junior Playmaker
  • **
  • Posts: 85
    • View Profile
Re: Lag when activating game object
« Reply #11 on: July 16, 2014, 05:11:26 AM »
We can't really flag anything as static, because the environment is moving and the player is in the same position most of the time. So everything is moving.

We just tested it on a iPhone 4 and there was horrible lag, it indicates that it is physics. We are going to look into it.
Game Designer, CEO, Artist
http://2ndStudio.com

DennisJensen

  • Junior Playmaker
  • **
  • Posts: 85
    • View Profile
Re: Lag when activating game object
« Reply #12 on: July 18, 2014, 07:05:51 AM »
Okay, so we tried different things:

1. Changing from 3d physics to 2d physics.
Result: Worse, not sure why. I then took around 40% of the CPU compared to the 4-5 % before.

2. Changing Timestep.
Result: None

3. Deactivating Gui text
Result: A lot better, apparently there is some spikes in performance, that where caused by 3d text that we used for GUI. This sucks.

I'm going to change the GUI now, let's hope it works :) and then test it on a Iphone.

It didn't really make sense, because we had like 10 drawcalls, and really simple graphics, so there is really no need to lag. We also use pool manager and does all the right things for performance.

Fingers crossed.
Game Designer, CEO, Artist
http://2ndStudio.com

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 12417
  • Official Playmaker Support
    • View Profile
Re: Lag when activating game object
« Reply #13 on: July 21, 2014, 05:47:07 AM »
Hi,

 It's your font probably! ?!

You should definitly use Ngui or 2dtoolKit ( or similar) instead of Unity' gui stuff ( until the hopefully good new version coming up shortly).
 Bye,

 Jean

DennisJensen

  • Junior Playmaker
  • **
  • Posts: 85
    • View Profile
Re: Lag when activating game object
« Reply #14 on: July 21, 2014, 09:38:14 AM »
Hi,

I did a lot, after changing the font to a sprite font using 2d toolkit it was much faster, but still not quite playable at iPhone 4.

I noticed we where using diffuse/transparent shader and I changed that to unlit/transparent. We havn't tested it yet, but we will tomorrow.
Game Designer, CEO, Artist
http://2ndStudio.com