Is there an elegant way to populate an area with RigidBody objects? - c#

I'm creating a penny pusher (like the game machines you find in arcades) game in Unity in my spare time and obviously that means I need to populate the shelf with coins so the player's spawned coins actually have function.
My issue is I haven't worked out how to spawn that many coins in an elegant way which won't either slow the game down with spawning so many objects at once or result in a lumped pile of coins rather than the neat spread I need.
I even tried speeding up the simulation by several times and dropping a pile of coins on the pushing shelf so they would "settle" before the player input was enabled. It didn't work.
So I ask if there's a good way to do this as part of the initialisation of the game and any Unity or C# function which would have the intended effect.
Thanks in advance.

If I understand your request correctly it really does dependent. The problem is that your game will be lagging when you have to many physic interactions. So even if you spawned them without alot of physic interactions, once u move one coin it will start lagging.
I would look for an alternative way then physics to handle them. Maybe you can think of a more simple calculation you do your self where you have less interactions going on each frame.
That beeing said a way to spawn the coins would be to calculate a random position at a fixed height (max height) and then find out what the lowest point is where the coin can sit on (so it does not share space with another coin). Now place it there. Even better would be to calculate if it could actually be placed there without instanly falling.
I think the best way would be having predefined "Coin Towers" (prefabs of them) where you have like 10 variations and then place them at different positions but do not allow to spawn any "Coin Towers" on another one. This would result in less physic interactions when spawned too.
May be helpful too: https://www.youtube.com/watch?v=pTz3LMQpvfA

Related

Unity Procedural Game Optimization

I am currently developing a 3d dungeon crawler which implements a procedural dungeon generation system which works perfectly fine. However the game is not very performant. I have tried a few optimization tricks I heard like:
Adding LOD Groups to each room prefab (this works perfectly fine), has impact on fps and batches
Tried addig static batching, I made every room prefab static and enabled dynamic and static batching in the player settings, however there is no change in the batches count or the saved batches count.
Tried addind occlusion culling but i cannot bake it before the game, as it's a procedural generated world.
The room prefabs are low poly rooms and are not even decorated yet, just the floor and walls and torches.
I should mention that i am using deffered lightning and I am activating the torch point lights as the players walks in the room. There aren't any baked lights everything is realtime. Every room prefab uses the same metrial and is not copied when the room is instantiated.
Some screenshots to help you understand better:
If you need anymore info, please don't hesitate to ask. Thanks you
Might be overkill, but have you thought about making the meshes a subscene / an entity using dots. Then just handle the colliders separately.
Hope that works for you.
If that is still having issues you may consider removing any colliders that are outside of a certain distance from you, and loading them in when you get close.

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.

Unity big scale world simulation performance problem.

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

Traffic simulation - large number of objects

I have a CSV with the following columns: time, carId, x, y. I am writing a script which will read the CSV and spawn car objects and simply update their positions over time based on the data.
Over the span of about 20 minutes, around 3500 car objects will have to have been instantiated. While they won't all be in the simulation at one point in time (once a car gets to certain points in my road network, it will disappear), but I want to be prepared for a situation where hundreds of car objects move through the network at once.
I'm still new to Unity and its rendering pipeline when it comes to optimizing for this big of a project. I know in some cases it's better to setActive(false) on GameObjects as opposed to destroy() and maybe this is one of them. What else should I consider when handling this many gameobjects in Unity?
What else should I consider when handling this many gameobjects in
Unity?
For the most part, this really depends on the amount of Objects or cars that will be displayed at the same time on the screen.
If it's just few cars then use Object Pooling to recycle the Objects.
You should also use LOD to optimize and reduce the number of triangles rendered for an object.
If it's a traffic simulation with hundreds of cars moving at the same time then you should use GPU Instancing which is now built into Unity. To enable GPU, use a Standard Shader and check the "Enable GPU Instancing" box.
After enabling it on the material, you can use Graphics.DrawMeshInstanced to instantiate the Objects and MaterialPropertyBlock to change how they look.
This sounds like a great opportunity to use Object Pooling. You are on the right track with using setActive.
Follow this short tutorial : https://unity3d.com/learn/tutorials/topics/scripting/object-pooling
It should reduce a lot of the lag you would get by instantiating/ destroying a lot of objects.

Floating rigidbodies script in Unity

I'm trying to create simple game where you jump around with ropes hooking to flying islands. I want islands to just float and when hooks himself to this island he begins to pull it to himself, but he is also able to swing on it. After he releases the rope islands slows down and stops (but I guess this is done by the rigidbody drag). Can you tell me how this script could work because I don't have any good idea of doing this.
You should disable gravity for the islands' rigidbody, first of all. That way, they will float, but things like inertia will still apply.
If you want your character to pull the islands to himself when grabbing them with a rope, you could use Rigidbody.AddForce(Vector3 vec3) and pass the vector opposite to the direction the rope is aimed at as a parameter (you should use Vector3.Reflect() to do that.)
This will get you started, but there are more ways to improve the effect, such as making the islands gradually slow down after pulling them. I'd suggest multiplying the island's velocity by a fraction of 1, in that case, but there are other ways to do it.

Categories