HOW TO CAUSE A LOT OF LAG IN UNITY

yes you read the title correctly.
SO i want to make a piece of code to make my game lag how would i do that?

there’s a million things you could do, first one to come to mind is use texture2d.apply ~ it’s really slow.
but a fair word of warning, one man’s lag is another man’s completely frozen game.

Easiest to control is probably to monkey with the Application.targetFrameRate

2 Likes

It depends on what you mean by “lag”. Lag could describe several different things but most commonly it describes network lag / latency. So single player games would not be affected by that. Lag may refer to a general bad performance / low fps or sudden performance drop due to garbage collection or other things.

So describe what exact behaviour you want to simulate. Note that it generally makes no sense to deliberately introduce issues, so when you actually want those issues, you should know and describe WHAT you actually want. If you can’t describe what you actually want, at least tell us the purpose. It’s your question, so make sure you explain your problem properly

1 Like

I’m assuming OP wants to simulate worst case performance of some part of his game design.

Colloquially most people call any type of time hitch to be “lag.”

I consider lag anytime a thing happens late: input, then delay, then action.

Low framerate over extended time I like to call chug or chugging.

And one or two frame stutters here and there I call hurk.

And finally a really long hurk is a multimedia moment.

I suppose it’s not unlike aeronautical turbulence: bumps, chop, thermals, wake turbulence, clear air turbulence, etc.

:slight_smile:

1 Like

You dont want your game to lag. So tell us what you actually want please. You can slow time, set a target frame rate, add screen/ camera shake, make a slideshow animation… but at no point you want the game to have bad performance intentionally. Even if you somehow wanted to test performance issues, artificially causing them is absolutely not the same thing as actually having them. To get worse frame times, you can simply add a dynamic loop that runs some iterations. On your specific computer, this would allow you to pretty precisely adjust how fast the game runs by wasting time each frame. But even if you do that to test certain things like how the game behaved on lower framerates, actual performance issues may, for example, be sporadically be caused by things like garbage collection and thus wont be as consistant as simply wasting performance each frame until you arrive at 10 fps or so.

So again… tell us what you actually want to achieve here.

2 Likes

When you use Thread.Sleep in one of your Components it will cause the main-thread to “lag”:

System.Threading.Thread.Sleep(10);

The 10 is the amount of milliseconds the thread is put to sleep.

I often use code as found below to identify and debug animation bugs:

int ms = UnityEngine.Random.Range(0, 30);
System.Threading.Thread.Sleep(ms);

It will cause a very unstable frame-rate and allows to spot code issues that rely on a smooth frame-rate.

4 Likes

You don’t need to do anything special, really, follow Unity’s tutorials and a lot of the advice on these forums and you’ll do just fine.

4 Likes