Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Which of these methods is the "cheapest"?

Discussion in 'Scripting' started by Not_Sure, Jun 26, 2015.

  1. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    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!
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    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).
     
    Not_Sure likes this.
  3. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    I had the same thought. And I would bet that the circle is the same as the sphere, but ignores the z plain.
     
  4. booiljoung

    booiljoung

    Joined:
    May 12, 2013
    Posts:
    57
    Collider is cheapest on Unity5.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Do you have rigidbodies on the colliders? Colliders that are meant to move should have rigidbodies.
     
  6. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    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.
     
  7. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,200
    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.
     
  8. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    I'm using 5. But I did not know that about 4.

    Thanks!
     
  9. Duugu

    Duugu

    Joined:
    May 23, 2015
    Posts:
    241
    Is it just me, or does "100 colliders on iPhone 1" sound impossible? :)
     
  10. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    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).
     
  11. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Turn off interactions between layers in the physics matrix for as many as you can. We saw rather large gains after doing this.
     
  12. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Unity can't run on iPhone 1 any more.
     
  13. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Ah, well I really just mean that I would like the game to run on lower end devices. iPhone 1 was just an example.
     
  14. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    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.
     
  15. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Okay, update:

    I attached this:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class ColliderSwitch : MonoBehaviour {
    6.     public GameObject myPlayer;
    7.     public GameObject myCollider;
    8.     public float minDistance;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.         myPlayer = GameObject.Find("Player");
    13.         myCollider = gameObject.transform.Find("Collider").gameObject;
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void Update () {
    18.         float distance = Vector2.Distance (myPlayer.transform.position, gameObject.transform.position);
    19.         if (distance < minDistance) {
    20.             myCollider.SetActive (true);
    21.         } else {
    22.             myCollider.SetActive (false);
    23.         }
    24.     }
    25. }
    26.  
    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.

    :)
     
  16. Not_Sure

    Not_Sure

    Joined:
    Dec 13, 2011
    Posts:
    3,541
    Thanks everyone for your help!
     
  17. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    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.