Performance problems with colliders on iPhone

Hello *,

I have the problem that the framerate of my game drops down when running on the iphone.

If I remove all colliders the game runs with 30 frames per second but with colliders between 4 and 10.

The game is a remake of an old arcarde game with planes and bullets.

I do not need physics and all colliders are triggers.

At the moment both the planes and bullets have a colliders but only the planes have a rigidbody too.

In the game there are a maximum of 10 planes and 30 bullets at the same time.

If a bullet hits a plane the following script in the prefab of bullet will be called:

function OnTriggerEnter (other: Collider){ 
   if (other.tag ==  "Opponent"){
	other.SendMessageUpwards ("ApplyDamage", 10, SendMessageOptions.DontRequireReceiver);
	Destroy(gameObject);
   }	
}

Please can you tell me if there is a way to gain framerate by optimizing the collision detection?

Thank you,
Ulrich

I believe you instantiate / destroy your bullet all the time ?

because to have already use quiet a lot of trigger / event things on ios game , i never got such drop in framerate.

so i am not sure the issue eventually came from trigger.
in your test without trigger are you also destroy / instance your bullet ?

the send message things also may be heavy if your using it very often in your update …

The object destruction (and implied creation), sendmessage, and tag comparison are all killers. Get rid of those if you can.

Don’t destroy objects at all, put them in a pool and reuse them.
Instead of tag comparisons, use layer based collisions.
Instead of sendmessage, do GetComponent and a direct method call.

Also reduce physics resolution and iteration as much as possible.

At least, that’s what I did for Oh Shoot! and it can handle a reasonable bullet hell on 1st Gen devices.

You say you don’t need physics? Does that mean you’re moving all rigidbodies yourself? Using optimized physics for that is probably faster and easier to tweak.

Hi giyomu,

in the test I have only removed the collider and the rigidbodies. I still instantiate every bullet.

You think I shold better activate / deactivate the gameobjects instead of instantiating?

Ulrich

Hi Jasper,

Thank you for your answer. I will try to remove the instantiate, destroy and compare commands.

I do not need physics at all because the opponent plane, when instantiated, will look and fly directly to the player with always the same speed. yes, I move the rigidbodys by myself (they will always get a new transform.position, transform.rotation).

Is it possible to check collisions between colliders if all objects do not have a rigidbody?

Ulrich

Are you using mesh colliders? They are slower than using the standard colliders, box, sphere, etc.

Quite strange. I have around 60 MeshColliders on my scene plus some other primitive colliders, and my game runs at 30 FPS. Also I’m instantiating objects every 1-2 seconds, but I don’t have drastical performance drops…

Hi *,

I only have box- and sphere colliders…

First I will try to remove all instantiating and then have a look what the framerate will say.

@Jasper: I’m new in Unity3D, please can you give me a short example for: “Instead of sendmessage, do GetComponent and a direct method call.”

I have uploaded a web-version of Retro Pilot (a remake of an old arcarde game) which you can find at: http://www.gzwo.com/htmlunity/retropilot.html

Every plane, cloud and bullet will be instantiated.

So you maybe can better see whats the reason of the performance problem…

Thank you all,
Ulrich

I would just put bullets or anything that is created/destroyed in a fairly timely manner in an object pool (just deactivate when you are not using it, and activate it when you need to use it).

The online GetComponent documentation explains it pretty well. Just do other.GetComponent(MyScript).DoSomething(), or other.GetComponent().DoSomething() if you would switch to C#.

The catch is that you need to be sure that the other object has the MyScript component before you call the method. You could first store the component in a variable and check whether it’s null, but that means you could be doing pointless component lookups.

If you make sure all your potentially colliding objects have MyScript attached (or a component that extends MyScript) then you can safely call the method without checking. Combine this with layer based collisions and you can be 100% sure which components the other has.

Checked out the web version. To me, it looks like all moving stuff in Retro Pilot can be driven by physics without much trouble. A 15 fps update with interpolation for enemy planes, bullets, and clouds might work just fine. But it doesn’t look like such optimization is really needed with so few planes and bullets moving around, if you use the other tricks.

Yesterday I have put all Objects which should be destroyed into a pool so I did not need to destroy / instantiate them every time. The framerate did not become much better.

Tomorrow I will try to use different layers to get less collisions and to remove the sendmessage-commands.

Please where can I adjust the physics rate?

Thank you all for your help,
Ulrich

Edit > Project Settings > Time > Fixed Timestep

other.SendMessageUpwards (“ApplyDamage”,

this is the slow way of using sendmessage. use version for a speed gain. but I still don’t see how this would slow it down as its only called in onCollisionEnter, so its not very frequent.

One thing to bear in mind.

The physics might not be slow.
Physics are done on the CPU.
Your other scripts could be taxing the cpu so much, that physics seems slow and gets faster when you remove colliders.

On iphone, use the profiler in xcode. It will tell you where the issue is. Enable it in appcontroller.mm

My suspicion is that physx will only take up about 1-2% :wink: and its your scripts that the big issue.

I played your game and it is very simplistic. The cause of the slowdown (true cause) must be profiled. You can do this in appcontroller.mm. Also set kFPS to 60 and accelelerometer to 1.0.

Question.

In unity player build settings have you put “fast but no exceptions” because otherwise you game will run too slow with error checking.

@hippocoder

I have enabled “fast but no exceptions”, the kFPS is set to 60 and the acceleroeter is set to 0.0.

On my PC I have around 3000 fps - and my PC is over 4 years old…

If I delete all rigidbodies and all colliders the game is still very slow. So I think you are right and the code will be the reason for slowing down the game. Sadly I have no idea at the moment what it could be. The code is not very long and I think I have not made such a big bug… :frowning:

Please what do you mean with “use version for a speed gain”.