My Game is stuttering

Hi Everybody,

i am pretty new to programming, Unity and all this iphone-stuff…

So… i have tried to created a 2D-game, with a character who has to jump from one moving platform to another. Because i have only one character that could collide with 10 platforms, i have solved the collisions via simple “if-then-scripts that are looking for overlapping positions” in update and late update.

My game has no physics, no skinned geometry, just plane polys for the background, platforms and the player, some of the textures are animated with a max of 8fps, ( i have altered the Animating Tiled texture - Extended-script) so that i can call 6 different UV-Animations via InvokeRepeat…

FixedTimeStep = 1
Iphone Script Call Optimization= Fast but no Exceptions
I have only 1 scene

The Game runs on my Ipod Touch 2G with about 55 to 59 fps
Unfortunatly every 15 seconds the fps drops for half a second to about 35 fps!

Because i am totally new to programming i have 18 scripts for my GameObjects and each uses its own function Update(), could this cause the slowdowns?

It would be very kind if someone would have a look at the stats of my profiler… is there something unusual?
(In the last part the max framtime is 106.1 … this should be the moment when the slowdown appears…)


iPhone Unity internal profiler stats:
cpu-player> min: 8.8 max: 15.0 avg: 11.6
cpu-ogles-drv> min: 0.9 max: 3.6 avg: 1.6
cpu-present> min: 0.8 max: 1.0 avg: 0.9
frametime> min: 16.0 max: 28.5 avg: 17.5
draw-call #> min: 13 max: 15 avg: 14 | batched: 0
tris #> min: 156 max: 190 avg: 165 | batched: 0
verts #> min: 152 max: 180 avg: 161 | batched: 0
player-detail> physx: 0.0 animation: 0.0 culling 0.4 skinning: 0.0 batching: 0.0 render: 7.9 fixed-update-count: 0 .. 0
mono-scripts> update: 2.9 fixedUpdate: 0.0 coroutines: 0.0
mono-memory> used heap: 356352 allocated heap: 356352 max number of collections: 0 collection total duration: 0.0

iPhone Unity internal profiler stats:
cpu-player> min: 8.7 max: 18.0 avg: 11.7
cpu-ogles-drv> min: 1.1 max: 3.7 avg: 1.6
cpu-present> min: 0.8 max: 2.1 avg: 0.9
frametime> min: 16.0 max: 37.1 avg: 17.6
draw-call #> min: 13 max: 15 avg: 14 | batched: 0
tris #> min: 156 max: 190 avg: 164 | batched: 0
verts #> min: 152 max: 180 avg: 160 | batched: 0
player-detail> physx: 0.0 animation: 0.0 culling 0.5 skinning: 0.0 batching: 0.0 render: 8.1 fixed-update-count: 0 .. 1
mono-scripts> update: 2.5 fixedUpdate: 0.0 coroutines: 0.1
mono-memory> used heap: 286720 allocated heap: 356352 max number of collections: 1 collection total duration: 5.0

iPhone Unity internal profiler stats:
cpu-player> min: -2.1 max: 48.4 avg: 15.6
cpu-ogles-drv> min: 1.0 max: 14.5 avg: 2.1
cpu-present> min: 0.8 max: 2.7 avg: 1.0
frametime> min: 13.4 max: 56.5 avg: 22.3
draw-call #> min: 14 max: 16 avg: 14 | batched: 0
tris #> min: 158 max: 192 avg: 167 | batched: 0
verts #> min: 156 max: 184 avg: 164 | batched: 0
player-detail> physx: 0.0 animation: 0.0 culling 0.5 skinning: 0.0 batching: 0.0 render: 11.9 fixed-update-count: 0 .. 0
mono-scripts> update: 2.5 fixedUpdate: 0.0 coroutines: 0.0
mono-memory> used heap: 335872 allocated heap: 356352 max number of collections: 0 collection total duration: 0.0

iPhone Unity internal profiler stats:
cpu-player> min: 8.5 max: 58.4 avg: 16.3
cpu-ogles-drv> min: 1.1 max: 4.4 avg: 1.7
cpu-present> min: 0.8 max: 1.9 avg: 0.9
frametime> min: 14.9 max: 69.0 avg: 25.1
draw-call #> min: 13 max: 15 avg: 14 | batched: 0
tris #> min: 156 max: 190 avg: 166 | batched: 0
verts #> min: 152 max: 180 avg: 162 | batched: 0
player-detail> physx: 0.0 animation: 0.0 culling 0.6 skinning: 0.0 batching: 0.0 render: 12.8 fixed-update-count: 0 .. 1
mono-scripts> update: 2.4 fixedUpdate: 0.0 coroutines: 0.0
mono-memory> used heap: 270336 allocated heap: 356352 max number of collections: 1 collection total duration: 4.8

iPhone Unity internal profiler stats:
cpu-player> min: 8.9 max: 18.3 avg: 11.3
cpu-ogles-drv> min: 1.1 max: 2.5 avg: 1.5
cpu-present> min: 0.6 max: 2.1 avg: 1.1
frametime> min: 15.8 max: 106.1 avg: 20.9
draw-call #> min: 14 max: 15 avg: 14 | batched: 0
tris #> min: 158 max: 190 avg: 165 | batched: 0
verts #> min: 156 max: 180 avg: 162 | batched: 0
player-detail> physx: 0.0 animation: 0.0 culling 0.5 skinning: 0.0 batching: 0.0 render: 7.9 fixed-update-count: 0 .. 1
mono-scripts> update: 2.1 fixedUpdate: 0.0 coroutines: 0.0
mono-memory> used heap: 311296 allocated heap: 356352 max number of collections: 0 collection total duration:
0.0

Thank You

Torsten

I know it sounds obvious, but is there any kind of event in the game that cycles roughly every 15 seconds?

Hi Andeeee,

no the slowest animation is called by an InvokeRepeating an runs two times per second… btw. i have found that i can reduce my Updates with this InvokeRepeating ;)) i know… i am noob :wink:

in the profiler i have seen that the stutter appears a lot of times when used heap gets near the allocated heap… so i have tried to use the pre-allocation-script that i have found here:

http://forum.unity3d.com/viewtopic.php?t=31838&highlight=preallocating

but that doesn’t eliminates the problem … my max. number of collections are now at 0 most of the time… but the stutter is still noticable … it looks like it changes the frecuency… and more smaller stutters appears…

i am trying now to insert a splashscreen so that i can be sure that the whole scene is preloaded…

hmmm… i keep trying…

thank you Andeeee!

cheers

Torsten

If the game actually pauses briefly (ie, doesn’t just run at low FPS for a while) then it is probably a memory allocation issue.

Without seeing all eighteen Update functions :shock: it isn’t easy to suggest exactly what is eating memory. However, you should look at your if-then tests to see if any of them involve allocating objects each frame, especially arrays or strings.

Perhaps you could post the Update (and LateUpdate, FixedUpdate, etc, if necessary) at least for your main character. It will make it easier to see if there are any GC-killing allocations taking place.

Hi Andeeee,

yep the game falls back from 59fps to 42-45fps for just one frame and is then back at 58/59fps… the GC has always made 1 collection at this time that lasts 4-5 ms …

So … i don’t use any arrays, i have deleted all my if-then position testing… none of my textures is animated anymore, all sounds are off… and i have only 6 platforms flying around…

my player is jumping in the air on a fixed y-value and the stutters are still alive :wink:

ok … i see what i can delete next and keep you informed…

thank you for your help!

cheers

torsten

help please!!

Ok i have deleted and deleted and deleted and the stutter didn’t disappeared…

So… i have created an empty scene :
plus 1 cube with this script for rotation:

function Update() {

transform.Rotate(Vector3.right*50 * Time.deltaTime);
transform.Rotate(Vector3.up * 150 * Time.deltaTime, Space.World);
}

AND i have created:

1 empty gameobject as fps-counter with this fps-script:

#pragma strict

// A FPS counter.
// It calculates frames/second over each updateInterval,
// so the display does not keep changing wildly.

var updateInterval = 0.5;
private var lastInterval : double; // Last interval end time
private var frames = 0; // Frames over current interval
private var fps : float; // Current FPS

function Start()
{
lastInterval = Time.realtimeSinceStartup;
frames = 0;
}

function OnGUI ()
{
// Display label with two fractional digits
GUILayout.Label(“” + fps.ToString(“f2”));
}

function Update()
{
++frames;
var timeNow = Time.realtimeSinceStartup;
if( timeNow > lastInterval + updateInterval )
{
fps = frames / (timeNow - lastInterval);
frames = 0;
lastInterval = timeNow;
}
}

The shader on the cube is an iPhone/Transparent/Vertex Color with the island Texture attached.

FIxed Timestep = 1;

#define kFPS 60.0
#define kAccelerometerFrequency 30.0

that’s all … and i got the same fps-drop every 10-15 sec… 59fps are dropping to 45fps and its definitly visible in the rotation…

Andeee!!! What am i doing wrong here?

I am testing on an ipod-touch 2G OS 3.0, Unity Iphone 1.5, Demoversion

???

cheers

torsten

ok the easy-test :wink:

empty scene with just the fps-script on an empty gameobject!
and after ±15 seconds the fps drops from 59.3 to 44…

is it the fps-script?

But i have seen these stutters without the script in my game …

?

double post - sorry

Maybe aiming for 60fps is a bit too high? I don’t know what causes the stutter, but if you set it to 30fps you should probably be fine. That’s what I always do anyway.

Don’t search any further than this :

because you’re allocating a new memory space in each frame,

this :

because GUI class is far from optimum performance,

and this :

because accelerometer test spamming is a framerate killer.

You’re on a 2G, after all :wink:

Remove your allocation spamming by caching your vars,
Avoid GUI class,
Avoid Accelerometer.

And this should put you a 60 fps on 2G as you expected.

Thank you n0mad!!

ok… first i delete this fps-script :wink:
unfortunatly my character moves via the accelerometer … so i have to test if i can lower the frequency anymore…

oh and i have found that it makes a big difference running the rotating-cube-scene with…

NSTIMER_BASED_LOOP instead of THREAD_BASED_LOOP…

@Smag

you know running the game at 60fps has two advantages… first the movement looks much better and secondly have i scripted the collision detection in Update() and LateUpdate()… so the more fps i have the better my collision detection works…

But at least if nothing else helps i will cap the fps :wink:

Thank You!

Torsten

@ n0mad

t h a n k y o u !!!

my game is flying!

No more stuttering(!) i have set the kAccelerometerFrequency to 10.
the fps-script is deleted - very few Garbage Collections now - 1 per 30seconds - and i am using NSTIMER_BASED_LOOPS…

iPhone Unity internal profiler stats:
cpu-player> min: 4.0 max: 7.9 avg: 5.7
cpu-ogles-drv> min: 0.9 max: 2.3 avg: 1.1
cpu-present> min: 4.2 max: 5.9 avg: 5.1
frametime> min: 15.9 max: 17.5 avg: 16.7
draw-call #> min: 12 max: 13 avg: 12 | batched: 0
tris #> min: 116 max: 148 avg: 123 | batched: 0
verts #> min: 112 max: 136 avg: 117 | batched: 0
player-detail> physx: 0.0 animation: 0.0 culling 0.4 skinning: 0.0 batching: 0.0 render: 2.9 fixed-update-count: 0 … 1
mono-scripts> update: 2.0 fixedUpdate: 0.0 coroutines: 0.1
mono-memory> used heap: 262144 allocated heap: 765952 max number of collections: 0 collection total duration: 0.0

O-o

Wow, thanks again!

Torsten

Great, 16.7 is pretty difficult to beat now :slight_smile: