Instantiated rigid body bringing the FPS to its knees?

So I’ve been having regular performance problems in my game (going from ~20fps to 3fps, which I tracked down to this line:

var ship : Rigidbody = instantiate (newShip), newSpawn.transform.position, newSpawn.transform.rotation);

is there something wrong with that line? The model and texture aren’t too big (around 200k total), so I can’t imagine it would be the .fbx that’s causing the problem.

Anyone else having trouble using instantiate with the iPhone?, or is there a more efficient way I can spawn my ships?[/quote]

What happens if its not an instance, its just a regular scene object?

Instantiating usually only damages the frame rate if you’re doing it lots (for example, bullets) or you’re instantiating a very poly heavy object :slight_smile:

For a situation like bullets, what would be the best way of handling them if you were going to have hundreds of bullets being fired around your level?

For lots of bullets, I think I would code a hitTest on single points instead (here, it would be the bullet’s center). There’s no real need to test each bullet’s enveloppe, imho.

Definitely not bullets…

What sort of poly count of an object would be considered poly heavy for the iPhone?

From the optimization notes in the manual, you should keep the number of vertexes under 10k per frame.

for polygon collision? > 0

No way you are going to have polygon collision for bullets and alike. Polygon collision is at best an option for simple world objects like the level itself etc, but anything with the least bit of detail needs capsule or box collider.

I recommend to look into the Sprite Manager as you will need it right now. Without batching your performance will die directly as you have worlds too many drawcalls (remain below 31) with distinct 3d models for bullets.

One other note I should mention,…because the models themselves range between 3-500 verts,…the framerate only drops during the actual instantiation, and recovers after a second or two…

@Dreamora: Thanks for the tip about collision. I was using a mesh collider on the models. I switched it to box collision, and now instead of droping to 3 fps, it only drops to 14. I think I might be able to live with that!

Sigh. No, I was mistaken.

I forgot that I spawn multiple enemies at later levels. Hopefully sprite manager will help…

one question I had…will Sprite Manager work with 3D models?

oh, and btw…I don’t have any bullets. The most I have on screen is 6 3d models of around 500 polys each

For 6 prefab instances, that’s only 3000 poly’s which the phone should be able to handle, but how many draw calls are involved overall in your scene, and what is the overall tris/vert count from the stats window (i.e. are the more meshes in the scene beyond the 6 prefabs)? Please report back this information.

How do I get the drawcall information? \

edit: n/m…figured that out…

with 3 models on screen (player + 2 opponents)

Drawcalls ~32
Tris: 5.5k
Verts: 7.3k

Used Textures: 17 - 6.4MB
Render Textures 0 - 0 bytes switches 0
Screen: 480 x 320 1.8 MB
VRAM usage: 1.8 MB to 8.9 MB (of 256 MB)
VBO Total: 20-219.5 KB
VB Uploads: 1 - 512 bytes
IB Uploads 1 - 80 bytes
Visible Skinned Meshes: 0 Animations: 0

I’m guessing that at later levels (when the opponents hit their max of 5 on screen at once) these will go up…

On your Game screen, there’s a Stats button up top. This will show you vram usage, draw calls, polys, etc.

At 32 draw calls, you are definitely going to have an issue on the phone and at 5.5K tris/7K verts, you are also pushing the limits of the phone. The first thing you want to do is make sure that for all meshes which share a common material, you are using the combine children script. Put them together under a single parent and then add the script to that parent. Otherwise you will get a draw call per mesh.

Then you need you’ll probably need to look at reducing the poly count of your models. Depending on your tool of choice (I use Maya), you should be able to take your models and through a combination of removing manifold geometry and running a reduce poly tool take them down some and without them starting to look bad. (There are many threads on this topic on the forum.)

Lastly, if you are using GUI Textures or GUI Text objects for your UI, you need to be very prudent with these as they add a draw call per object. If you have a text based HUD, consider implementing it as a single GUI Text object using a single string that concatenates the text together using tabs and line feeds (there are a couple threads on this topic on the forum).

Overall you should try to get your draw calls down to definitely under 20 and the lower the better. Your overall tris count should be under 7K. At these levels you should be able to eek out about 20-25FPS on a 1G phone based on my testing. Assuming your scripts are optimized. You can turn the profiler on in Xcode and get good diagnostics for the phone (there is a thread on how to do this on the forum as well).

By the way, you may want to wait until the end of the week before messing with combining the meshes. Unity iPhone 1.1 is supposed to come up this week and is likely to include optimizations on this front.

Thanks Hanul! That’s what I was afraid of.

How do you use Combine Children with instantiated objects?

Once you have constructed the object hierarchy you can add the script to it using the AddComponent method.

I think I got how that works.

You think I would get better performance if I converted all of the models to sprites and used Sprite Manager instead?

Sprite Manager is designed for 2D sprites. There is an technique that PirateNinjaAlliance (maker of Zombieville USA) discusses on a few threads here where he animates 3D sprites as different parts of one single mesh. This might be ideal for you. Take a look at the discussion.

As I said, I would also wait a week or so if you can, 1.1 is supposed to have some optimizations which make draw calls much less onerous.