- Home /
Reducing Lag in Game?
So Iv been working on my game for some time now and lately im starting to realize its becoming really laggy. I was wondering if there is any way to reduce this lag? My level only has 3 houses, my main character, and the base area for the player to walk on and houses to stay on. I was wondering if I scale down the whole level and everything inside of it like objects, houses, everything, down to really really small size, would that reduce the lag in my game?
I am not using High poly models, Im only using the low poly models iv been working on, however they are all FBX files. But really my game is pretty much like a N64 game, In terms of Models, textures, and animations. but still it lags for some reason, and I am becoming worried because I still need to add a lot more things to the level such as other characters and more objects, and collision features and other scripting stuff. Lagging at an early stage is not a good sign.
Old post I know, but in case others run by here, it's worth noting that the scale of things in your game is nothing to worry about. If you decide to up everything 100 fold, so your game goes from -1000 to 1000 ins$$anonymous$$d of -10 to 10, it will not cause lag.
Coordinates are simply that, coordinates. It doesn't require more memory, nor is it slower to work with large floats than it is to work with small floats.
I am running into the same issue with my scene and game. Restarting Unity has no effect at all. I don't have the greatest of computer nor the worst. $$anonymous$$aybe a little below todays average in terms of ram and performance but I can play Assassins Creed Black Flag on low settings with $$anonymous$$imal lag so Im assu$$anonymous$$g that Ive done something to effect the performance somehow. As far as I know all of my models are .fbx as well which is what Ive read you should use.
I have between 20 -25k trees with colliders the map is a 3000 by 3000 height 300. In areas with little to no models such as buildings the lag is $$anonymous$$imal or non existant but at my Church Yard which has of course the Church and behind is the grave yard the lag is fairly bad.
I made a development build of my game which is 406 mb. Not even a gig so I figure it shouldnt be as bad as it is. When I run the build if I set it to windowed at 800x600 it is decent with only a little lag in the church yard.
Any advice or more info need to give advice?
If anyone is still having this problem I just finished making my game run smoother by changing my Update functions to contain as little intensive work in them as possible. Here's what I mean, let's say that you control your character's inputs through the update function. If you ever want to take control away from your character (for a tutorial or cutscene for example) it might seem reasonable to set up a boolean canPlayerMove and set it to true or false depending on the situation. Your script would look something like this:
void FixedUpdate () {
if (canPlayerMove = true) {
if(Input.GetKey (KeyCode.A)) {
transform.position += Vector3.left * movementSpeed * Time.deltaTime;
} else if (Input.GetKey (KeyCode.D)) {
transform.position += Vector3.right * movementSpeed * Time.deltaTime;
}
}
}
// This function let's the player move
public void PlayerStart () {
canPlayerMove = true;
}
// This function prevents the player from moving
public void PlayerStop () {
canPlayerMove = false;
}
However, now your Update method is checking whether or not canPlayerMove is true every single frame. Stuff like this is what will lag your game, so finding creative solutions to these types of problems is going to be what helps you. A solution to this problem might be turning the canPlayerMove boolean into an integer that is either 1 or 0 and multiply it to the transform.position. Here's what that would look like:
void FixedUpdate () {
if(Input.GetKey (KeyCode.A)) {
transform.position += Vector3.left * movementSpeed * Time.deltaTime * canPlayerMove;
} else if (Input.GetKey (KeyCode.D)) {
transform.position += Vector3.right * movementSpeed * Time.deltaTime * canPlayerMove;
}
}
// This function let's the player move
public void PlayerStart () {
canPlayerMove = 1;
}
// This function prevents the player from moving
public void PlayerStop () {
canPlayerMove = 0;
}
}
The resulting code is functionally the same, but now you are checking for less things and your game would run more smoothly. I hope this helps if anyone needs it.
PS: This theoretical setup I explained was actually something in my game and after making these changes it heavily improved the performance.
While I agree with your sentiment, I find it hard to believe that that particular example made any appreciable difference in performance. Sure, you've removed one (incredibly fast) boolean test each frame. But you've added one (very fast) multiplication and an implicit int -> float cast every frame ins$$anonymous$$d!
if i change The Update() Function to FixedUpdate() then can i resolve this type of problem
me i lag and i have just 3 house with a road and in the stats its write i do 117 FPS but i feels the lag
Answer by mpavlinsky · Jan 05, 2012 at 08:31 PM
I wouldn't worry too much, I'm sure you can get this resolved and add everything you want especially if it's not graphics intensive. You probably just need to adopt more efficient scripting and graphics practices.
If you have Unity Pro the profiler built into Unity is good and can give you great insight into what is bogging you down. Barring that you can find some information about doing graphics optimizations here, and there is some information about optimizing scripts here.
You are right, looks like I was worried for no reason. I just closed unity and reopened it and it runs smooth again on gameplay, it must of become laggy when I was importing my models for some reason. Even better I just made a build of the level Im working on to test it out, Its file size is pretty small (only 8mb, so far)
surprisingly it runs extremely smooth in the actual build. I feel a lot better now. haha.
Answer by chesterhilly · Jun 15, 2015 at 09:10 AM
In Mobile Development, change the materials to mobile shaders. This really can improve framerates but the textures look not as good
Answer by chesterhilly · Jun 15, 2015 at 09:11 AM
By the way, I just fixed my lag by about 5 fps to about 50 fps. I had the annoying little error that says "NullReferenceException...." This was because I had to put a gameobject and make it into a prefab and then assign it to the other prefab variable.
THIS SHOWS THAT WHEN YOU FIX ERRORS IT CAN REALLY IMPROVE FRAMERATES
no one should ever leave any errors in any game! Not only does it obviosly slow a game, but they can also make your game do wierd stuff like skip other non-related sections code.
Answer by toddisarockstar · Apr 21, 2017 at 03:27 PM
I'm glad you found your problem was just in the editor! anyways looks like others are following this question. So i thought i would add for anyone who does have a level too large. First of all scale does not matter! what matters is the number of faces and amount of textures sent to the video card! A cube as small as a rock or scaled large as a building requires the same amount of processing power to display!
so generally you never use more faces in your models than needed and as less textures as possible.
Unity has something called batching which by default is running during your game. it does help with ignoring textures not in use and reducing shader quality at distances but Its really not enough for a really big world.
there is a technique called LOD which uses simpler meshes at a distance. https://docs.unity3d.com/Manual/LevelOfDetail.html
there is also a setting to reduce texture quality at a distance https://docs.unity3d.com/ScriptReference/Texture-anisoLevel.html
of course you can do stuff with code for your game too. an example would be simply disabling game objects or complex meshes when you know they are out of view with code. Such as the detailed inside of a building or room when the user is outside!
Answer by Joe-Censored · Apr 21, 2017 at 04:20 PM
Just to add another performance tip, while developing I'm often a bit liberal with the debug.log calls, outputting different values where I'm trying to debug my code, and then sometimes not immediately disabling them when issues get figured out with the intention of cleaning up all these log calls before release. While trying to figure out my own performance problems though I discovered that Debug.Log outputs are actually very expensive, much more than I expected. A few Debug.Log calls per update can bring your game to a crawl.
Your answer
Follow this Question
Related Questions
Lag issues with unity3d + Arduino (Xbee) 0 Answers
unity application getting slow 0 Answers
Unity 3D - Model Failure! Please HELP ME!!! 0 Answers