- Home /
Is there anyway to optimize my physics based game?
Sorry, my question is fairly vague. My game is about stacking objects. The problem is, while stacking high is not the only way to obtain points, it is still one viable way to get a high score in my game. This leads to many rigidbodies stacked on each other. I noticed on my phone that after stacking quite a few objects, the performance went down a bit. Not too much but still it was a noticeable drop. I assume performance will continue to go down as more objects are stacked. Did I just poorly design my game or is there something I could do when handling many rigidbodies? I'm trying to think of ways to keep my idea but I don't want to players to be turned off by poor performance, it's like they would be getting punished for doing good in the game. I've already made it a point to stay away from convex mesh colliders, all of my colliders are compound colliders or just a single primitive. I appreciate any suggestions, thanks.
Are you handling any physics inside Update
? If so then switch to FixedUpdate
- which is meant for physics/rigidbody-related stuff. - It's a bit hard to tell what's causing the drops since you didn't show us any code. If you're on Unity Pro use the profiler to identify the bottlenecks.
Hey thanks for the reply. I would have showed code if I thought it would help. There really isn't anything to show as there really isn't any physics based code. Perhaps there should be though, that's kind of what I'm trying to figure out. Perhaps there is a technique I don't know when dealing with many active rigidbodies. It seems unlikely though. The game is about stacking objects of varying shapes, the tower of objects that is placed is constantly shaking and even swaying as the player builds. It's decently fun to play but as you might imagine, the higher the player stacks the larger the impact on performance.
do you really need all the stacked objects to be active at the same time? - if not, then you might consider disabling those you don't need.
In my opinion, the only way to prevent the decrease in performance is not to use a rigidbody for every stacking object.
$$anonymous$$aybe you could think about having a rigidbody to the whole stack and a rigidbody to the last object added to the stack. In other words, simplify the physics of all previously added objects to a single "accumulative" object, and then have that, interact with newly added object that will also have a rigidbody attached, for the sake of interaction, and then removed after its effect has been accumulated on the main stack.
Hope this helps.
Hey thanks guys. I agree, it's looking like I need to start grouping or manipulating the objects in some way. I'm not sure about a singular accumulative object for my game but this really does help push me in the right direction. I'm thinking I'll group the objects in chunks but do it in a way that the number of chunks never exceeds a certain threshold. I'll have to play around with the idea.
Answer by salex100m · Nov 17, 2013 at 06:05 PM
Hey, I just ran into this same problem. My code isn't quite complete so my GOs are all congregating on top of each other. When more than 4 end up at the same position, they game slows down to mud. It isn't related to the number of objects either because if they are not stacked, the game runs fast.
I strongly suspect it is because each object has a mesh collider attached and those colliders are triggering themselves up the wahoo when they are stacked.
I suggest you turn off collision and triggering if possible when your items are stacked. After all, collisions are intended to bounce objects off each other.
Let me know if that worked.
Yes, that is the problem. I just saw this question got bumbed but i didn't see any new post, so i checked the moderation queue and i've fould your answer ;)
The problem when you stack several physics objects they push each other all the time. A single impulse could go several times up and down through the whole stack.
If your game mechanics allows it, just "disable" single elements when they come to "rest" by setting is$$anonymous$$inematic to true or by destroying the Rigidbody component.
I'm currently looking at this issue, like I said. I've counted exactly five objects can stack with no visible fps lag. As soon as I add a 6 object, it goes to hell.
If you turn off "is Trigger" then they just bounce off each other.
If you want to stack them, just forget the physics colliders and maybe just check the difference from the transform.position of each object. If the object's center point is very close to another object's they are stacked right?
Answer by meat5000 · Oct 06, 2013 at 10:24 AM
Set Fixed TimeStep to 0.01 in Time Manager.
Consider:
function OnCollisionEnter()
{
//if collision object is stackingObject
//Parent Object
//Make Kinematic
}
Answer by Jamora · Oct 06, 2013 at 02:09 PM
The problem could also be because of excessive draw calls. (Check the stats window in the Editor, next to Maximize on Play.) If the number for draw calls increases every time you add an object, you will need to consider sharing materials.
Hey thanks for the input. I am using shared materials, my game stats look pretty good and the draw calls are never over 10 so far.