Which of these methods is the "cheapest"?

Hi everybody,

So I’m doing some optimization for moving obstacles in my 2d game. At first the FPS dropped like a stone when I had more than 10 obstacals at once.

When I looked it up physics were by far the biggest CPU hog. So I redid the colliders for the obstacles and made them much simpler which made it run much better.

However, it’s still not quite where I want it.

So I’m thinking I want to turn the colliders on and off. And it seems like I can do this one of three ways.

  1. I could use a very simple collider as a trigger to turn on the detailed collider. I’m not sure if circles or squares are cheaper though.

  2. I could track the distance from the obstacles to the player and then turn on the collider when it gets within a certain range.

  3. I could compare the x and y coordinates of the player and the obstacle, then turn it on when both are close enough to one another.

I’m guessing #3 is the cheapest, but I figured I’d ask and see if anyone has an idea.

Thanks!

I’m pretty sure your solutions 2 and 3 are the same, and I’m pretty sure that’s exactly what the sphere collider does. The sphere collider is just the range to a point, so that just does a range check (I assume).

1 Like

I had the same thought. And I would bet that the circle is the same as the sphere, but ignores the z plain.

Collider is cheapest on Unity5.

Do you have rigidbodies on the colliders? Colliders that are meant to move should have rigidbodies.

Nah, just the character controller.

My FPS is terrible. I’m getting about 70 with 100 obstacles on my 5 y/o Samsung laptop with 4 gigs of RAM and 2.5 Ghz Dual core AMD. It’s just that I want to make sure that it’ll work on low end tablets and phones with 100 or so. Like iPhone 1 low end.

Is this Unity4 or 5?

If it’s Unity 4, and your obstacles are moving and have colliders, you have to put kinematic rigidbodies on them, or your performance is going to crash hard - Unity 4 assumes that everything that has a collider, but no rigidbody, is never going to move, and optimizes for that. This means that whenever you move one, Unity has to rebuild a lot of it’s internal representation of the world.

The same thing shouldn’t be a problem in Unity 5, as they stopped making the same assumption - it was messing with too many people, and wasn’t very obvious.

I’m using 5. But I did not know that about 4.

Thanks!

Is it just me, or does “100 colliders on iPhone 1” sound impossible? :slight_smile:

I personally prefer event driven calculations rather than polling calculations.

Your options 2 and 3 are going to be polling on some interval (probably Update) for every detailed collider.

Where as option 1 will signal a message on the single moment when you intersect it (sure the physics engine is doing a bunch of continuous tests… but it’s a lot more efficient at doing that rather than you doing distance tests).

Turn off interactions between layers in the physics matrix for as many as you can. We saw rather large gains after doing this.

Unity can’t run on iPhone 1 any more.

Ah, well I really just mean that I would like the game to run on lower end devices. iPhone 1 was just an example.

Well if it’s Unity 5, the physics are all multithreaded and it’s a much faster version of physx. You do not need rigidbodies on colliders in Unity 5 if you want to move them.

Okay, update:

I attached this:

using UnityEngine;
using System.Collections;

public class ColliderSwitch : MonoBehaviour {
    public GameObject myPlayer;
    public GameObject myCollider;
    public float minDistance;

    // Use this for initialization
    void Start () {
        myPlayer = GameObject.Find("Player");
        myCollider = gameObject.transform.Find("Collider").gameObject;
    }
  
    // Update is called once per frame
    void Update () {
        float distance = Vector2.Distance (myPlayer.transform.position, gameObject.transform.position);
        if (distance < minDistance) {
            myCollider.SetActive (true);
        } else {
            myCollider.SetActive (false);
        }
    }
}

and it made a MASSIVE difference.

I can now run like 500 with minimal dip in performance so long as they aren’t all in range.

:slight_smile:

Thanks everyone for your help!

I would suggest looking at Geographic Grid Registration, basically creating an imaginary grid of whatever size squares you want to assume. Then attach a behavior to your objects that register them with whatever “block” they are in on the grid. When your player moves between blocks just activate and deactivate colliders as needed, only those within the player and neighboring blocks if necessary. It’s a very efficient pruning mechanism as you can quickly determine which groups of objects you need to interact with.