Unity big scale world simulation performance problem. - c#

We are working on simulation game. We have about 25000 objects at world. All has 1 unity c# script. If we activate empty update function we get 15 fps if we activate empty fixed update with 0.02 time scale we get 1-2 fps at average spec computer. In Will we need to do something on update function. So need some help for this performance problem.
In that case, what can we do for performance?
Thanks for advices and help.

Well even though your question is broad i will try to give some tips. My guess is you are doing some heavy calculations in your Update and this causes fps problems. Also rendering 25000 object at the same time would cause fps issues. First i suggest using Unity Profiler and Unity statistics to find out what is causing the issue. Then my suggestions would be based on the problem i assume:
Use Coroutines instead of doing all the calculations in Update.
Use Occlusion culling for rendering as minimum objects as possible. You do not need to render objects which are not in Camera frustum.
If you have meshes which are detailed use level of detail if you have the chance.
If you narrow down your problem i am happy to help further. Good luck!

The very need to make a call to Update() is one of the major slowdowns in Unity. That's why they are now pusing towards Entity Component System where there is no need for such jumps.
If all the objects are having the same script, it should be quite easy to modify the code so that only 1 object has the script, but operates (i.e. via a for (;;) loop) on all other objects. This should bring massive improvements already.
Aslo if theres any chance your objects share meshes - do use Instanced Mesh Rendering it is faster by two orders of magnitude

Related

Why does activation phase of a scene being loaded in Unity take too much time?

I'm new to Unity3D. I am trying to make my own FPS game in Unity 2020.3. I have to make a lot of maps (scenes). So far I discovered a problem with LoadSceneAsync.
What I know from Unity docs: When AsyncOperation.progress variable is between 0 and 0.9, the engine is loading resources and stuff for the new scene. When it reaches 0.9, it starts activating the new scene (deleting old objects and adding new ones + running Awake and Start methods?).
The problem is that the activation phase takes A LOT of time. For example: when the loading phase takes only a few frames, the activation phase takes about 10 or a 100 times more frames to do its thing.
I tried running the game on another devices and I saw no comparable change in loading speed. Is this a Unity problem or am I using it wrong? Or are those device that I tested the game on just not good enough?
I have no idea why this is happening. I pulled up profiler but I can't understand what exactly is going on. Can somebody please explain what makes this so slow and how to improve its performance?
What version of Unity are you using? When I look at your profiler log I see Prefabs.MergePrefabs (834,62ms):
When I google that I find an answer here that seems to imply that this has something to do with nested prefabs, or prefabs used in a GameObject hierarchy.
I honestly am not sure what the answer is saying to do to resolve the issue, but search results also turned up this Unity issue that says a slow Prefabs.MergePrefabs is:
Reproducible with: 2019.3.0a1, 2019.3.12f1, 2020.1.0b8, 2020.2.0a9
The issue tracker states that the issue is:
Fixed in 2020.2.0b1
Fixed in 2020.1.8f1
Fixed in 2019.4.24f1

Are My Unity 2D Transform.Position Units Changing Between Builds?

I've put together a little Git repo to share with you some scripts from the Unity 2D platformer I've been working on. I have written some basic WASD movement control in the Movement.cs script (under Scripts/Character in the repo) and it is working really well. It does everything I need it to do, and while it is basic, I intend to polish it up a bit over the course of the development of the game. However, I've been noticing that every time I build the game, every moving entity moves at a faster speed. The "AI" (yes I use the term very loosely) that I programmed into enemies like the Chompas and BadBirds seems to be far too fast or far too slow, as are user-controlled movements and animated powerup bubbles.
Now, I believe I've traced it back to the way I create translations; I add vectors to Transform.Position whenever I need to move an object or entity. These vectors accept float values as their parameters, and seeing as I'm not entirely clear on what those values represent, I feel that may be where the issue lies. Are these distance values representative of some dynamic system of measurement that might be changing between builds? If so, how might I standardize my distances? Am I totally off the mark? Lol. Any and all help is greatly appreciated.
To be perfectly clear, this issue occurs every time I hit play, regardless of whether or not changes have been made. Thanks again!
Git repo
Try multiplying your movement by Time.deltaTime.
If you don't do this (which it doesn't look like you are), then all your GameObjects will move at inconsistent speeds. The reason for this is that void Update() get's called once every frame, and the amount of frames that get drawn every second is variable (sometimes your game will run a little faster than at other times), and so your speed will be variable if you don't multiply it by Time.deltaTime (Time.deltaTime is the amount of time that has passed since the last frame, see the documentation)
Another option is to use Unity's built in physics system with FixedUpdate(), which get's called at a fixed rate.
I would strongly recommend to follow online tutorials for how to make a platformer controller etc., such as this one. Doing this will help teach you good practices and how to avoid common errors.

C# - Move objects in console application with different speed

I am studying C# at the moment and I had an exam recently. I had to create a console game that has moving objects. I pretty much managed to do everything that was required, except I couldn't think out a way how to make each game object to move at different speed (actually there are only two moving objects, floors and the character), one of the tasks was to make powerups that increase either the character speed or the floors speed.... I couldn't think out a way how to modify their speed separately.. I am currently trying to finish the game but I cna't get my thoughts around this, my "endgame" is to have two variables charSpeed and floorSpeed... Could anyone explain to me how I can achieve this?
Thanks!
My code - http://pastebin.com/TkPd37xD - it's currently a mess, I just want to figure out what is the logic behind what I want to do. A "general solution", here I have only 2 objects, what if I want to change the speed of 10 objects?
P.S. No Classes, I can go up to Structs, I have not worked with Classes yet.
P.S.S. I take any kind of advices or criticizm about my code, so anything is appreciated since I am still learning, but my main concern at the moment is how to solve the problem at hand.
I would use a Timer for each different levep speed.
The callback of the timer would execute the logic of the movement.
This will certainly required a refactoring of your code ;)

Optimization of a GC language, any ideas?

I'm a pretty big newbie when it comes to optimization. In the current game I'm working on I've managed to optimize a function and shave about 0.5% of its CPU load and that's about as 'awesome' as I've been.
My situation is as follows: I've developed a physics heavy game in MonoTouch using an XNA wrapper library called ExEn and try as I might I've found it very hard to get the game to reach a playable framerate on an iPhone4 (don't even want to think about iPhone3GS at this point).
The performance degradation is almost certainly in the physics calculations, if I turn physics off the framerate jumps up sharply, if I disable everything, rendering, input, audio and just leave physics on performance hovers around 15fps during physics intensive situations.
I used Instruments to profile the performance and this is what I got: http://i.imgur.com/FX25h.png The functions which drain the most performance are either from the physics engine (Farseer) or the ExEn XNA wrapper functions they call (notably Vector2.Max, Vector2.Min).
I looked into those functions and I know wherever it can Farseer is passing values by reference into those functions rather than by value so that's that covered (and it's literally the only way I can think of. The functions are very simple themselves basically amounting to such operations as
return new Vector2(Max(v1.x, v2.x), Max(v1.y, v2.y))
Basically I feel like I'm stuck and in my limited capacity and understanding of code optimizations I'm not sure what my options are or if I even have any options (maybe I should just curl into a fetal position and cry?). With LLVM turned on and built in release I'm getting maybe 15fps at best. I did manage to bring the game up to 30fps by lowering the physics precision but this makes many levels simply unplayable as bodies intersect one another and collapse in on themselves.
So my question is, is this a lost cause or is there anything I can do to beef up performance?
First of all, love your game on Windows Phone 7!
Secondly, I don't see anything out of the ordinary in your profiler output. I did a quick and dirty performance analysis of the Farseer engine once (running in .net) and came up with similar results. It almost looks like you have a slowdown that is proportional across the board and may be due to mono itself.
I suppose you follow the performance hints in http://farseerphysics.codeplex.com/documentation already :-)
The most important thing seems to be
to reduce complexity for the
collision detection calculations,
i.e. not the visual but the colliding
shapes. In Unijty3D they are called
colliders and you can attach a simple
cube as a collider to a complex human
body. I don't know anything about
Fareer but they probably have similar
concept (is it called body?).
If possible, try to replace your main
character or other complex objects by
easy cubes and check if fps raises.
Compiler switches sometimes leverage performance. Be really sure that there are no debug settings activated (I got up to 30 times slower code in a C++ library project). Ensure that armv7 optimisation is turned on and -O3 or -Os
Watch out for logging statements as they are extremely expensive on iPhone
[Update:]
Try to decrease the number of actively calculated AABBs just to find out which part of the physics engine causes the trouble. If it's the pure number follow FFox' advice.
What is about other platforms? Where did you perform the testing during the development phase, on simulator? Which one? Any chance to get it running on Android or Android simulator or Windows Phone? This would give you a hint if it is an iPhone specific problem.
Ah, I just saw that ExEn still is in pre-release state and the final will be launched on July 21th as OS. IMO this changes the situation: If your App is running fine on some other comparable platform, then just wait for the release and give it a new try. Chances are pretty well that there is still debugging code in the pre-release you are working on.

Collisions in a real world application

Here's my problem. I'm creating a game and I'm wondering about how to do the collisions. I have several case to analyze and to find the best solution for.
I'll say it beforehand, I'm not using any third party physics library, but I'm gonna do it in house. (as this is an educational project, I don't have schedules and I want to learn)
I have 2 types of mesh for which I have to make the collisions for :
1) Static Meshes (that move around the screen, but does not have ANY animation)
2) Skinned/Boned Meshes (animated)
Actually I have this solution (quite hacky :|)
First of all I have a test against some bounding volume that enclose the full mesh (capsule in my case), after :
1) For the static meshes I divide them manually in blocks (on the modeler) and for each of these blocks i use a sphere/AABB test. (works fine, but its a little messy to slice every mesh :P) (i tried an automatic system to divide the mesh through planes, but it gives bad results :()
2) For the animated mesh ATM i'm dividing the mesh at runtime into x blocks (where x is the number of bones). Each block contain the vertex for which that bone is the major influencer. (Sometimes works, sometimes gives really bad results. :|)
Please note that the divide of the mesh is done at loading time and not each time (otherwise it would run like a slideshow :D)
And here's the question :
What is the most sane idea to use for those 2 case?
Any material for me to study these methods? (with some sourcecode and explanations would be even better (language is not important, when i understand the algorithm, the implementation is easy))
Can you argument why that solution is better than others?
I heard a lot of talk about kd-tree, octree, etc..while I understand their structure I miss their utility in a collision detection scenario.
Thanks a lot for the answers!!!
EDIT : Trying to find a K-Dop example with some explanation on the net. Still haven't found anything. :( Any clues?
I'm interested on HOW the K-Dop can be efficiently tested with other type of bounding volumes etc...but the documentation on the net seems highly lacking. :(
Prior to doing complex collision detection you should perform basic detection.
Using spheres or rectangles as bounding volumes is your best bet. Then if this detects a collision, move onto your more complex methods.
What I'm getting at is simple is often better, and quicker. Wrapping bounding volumes and splitting meshes up is costly, not to mention complex. You seem to be on the right track though.
As with game programming there are multiple ways of collision detection. My advice would be start simple. Take a cube and perfect your routines on that, then in theory you should be able to use any other model. As for examples I'd check gamedev.net as they have some nice articles. Much or my home made collision detection is a combination of many methods, so I can't really recommended the definitive resource.
The most common approaches used in many current AAA games is "k-DOP" simplified collision for StaticMeshes, and a simplified physical body representation for the SkeletalMeshes.
If you google for "kDOP collision" or "discrete orientation polytopes" you should find enough references. This is basicly a bounding volume defined of several planes that are moved from outside towards the mesh, until a triangle collision occurs. The "k" in kDOP defines how many of these planes are used, and depending on your geometry and your "k" you can get really good approximations.
For SkeletalMeshes the most common technique is to define simple geometry that is attached to specific bones. This geometry might be a box or a sphere. This collision-model than can be used for quite accurate collision detection of animated meshes.
If you need per-triangle collision, the "Separating Axis Theorem" is the google-search term of your choice. This is usefull for specific cases, but 75% of your collision-detection needs should be covered with the above mentioned methods.
Keep in mind, that you most probably will need a higher level of early collision rejection than a bounding-volume. As soon as you have a lot of objects in the world, you will need to use a "spatial partitioning" to reject groups of objects from further testing as early as possible.
The answering question comes down to how precise do you need?
Clearly, sphere bounding boxes are the most trivial. On the other side of the scale, you have a full triangle mesh-mesh collision detection, which has to happen each time an object moves.
Game development physics engine rely on the art of the approximation(I lurked in GameDev.net's math and physics forums years ago).
My opinion is that you will need some sort of bounding ellipsoid associated with each object. An object can be a general multimesh object, a mesh, or a submesh mesh. This should provide a 'decent' amount of approximation.
Pick up Christer Ericson's book, Real-Time Collision Detection. He discusses these very issues in great detail.
When reading articles, remember that in a real-world game application you will be working under hard limits of memory and time - you get 16.6ms per frame and that's it! So be cautious of any article or paper that doesn't seriously discuss the memory and CPU footprint of its algorithm.

Categories