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

Is it better to add more colliders or to add more logic to one collider?

Discussion in 'Scripting' started by boby-bobs, Apr 3, 2016.

  1. boby-bobs

    boby-bobs

    Joined:
    Oct 18, 2013
    Posts:
    53
    I have some simple AI script that uses MoveToward and LookAt to seek an object and then take it somewhere.

    With LookAt and MoveTowards in tandem the AI moves toward the object and then rotates towards it, then continues to LookAt it. The object is on the ground 90% of the time so this causes the AI to force itself into the ground to obtain the object.

    To fix this I need to do one of two things

    1. Create another collider, and OnTriggerEnter, say "OK, now it's within grabbing range so I should grab it"
    2. Add more logic to the current collider, but constantly check the distance to see if its close enough to grab

    Which will have a lower impact on performance?
     
  2. GameDevRick

    GameDevRick

    Joined:
    Oct 8, 2011
    Posts:
    269
    I always try to make everything event driven.
    The first approach is efficient using the OnTriggerEnter event.
    The latter approach is not feasible unless you have other compelling reasons for having the distance check in place.
    You should keep it as simple and with as little overhead as possible, so use the built in events.
     
  3. boby-bobs

    boby-bobs

    Joined:
    Oct 18, 2013
    Posts:
    53
    So Vector3.Distance is slower than the entire collider/tigger/event system?
     
  4. GameDevRick

    GameDevRick

    Joined:
    Oct 8, 2011
    Posts:
    269
  5. boby-bobs

    boby-bobs

    Joined:
    Oct 18, 2013
    Posts:
    53
    Thanks for the link
     
  6. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    Both a distance check and an additional trigger box to check collision would be very fast. These are not the kinds of things that will slow down the game in any noticeable way.

    That being said, with fewer colliders in the scene, there's fewer colliders that might make your raycasts and other colliders hit the wrong thing. The distance check will never interfere with anything else.

    Because of that, I'd go with the distance check in your situation.

    Also not that comparing square distances is faster than comparing distances, as you can get rid of a square-root operation. That difference will only be noticeable when you're doing very many distance comparisons per frame.
     
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    In this case I'd use the distance check.

    Most of the time collider checks are more efficient. PhysX does some clever intense stuff to mean distance checks only happen on close objects.

    However in this case you know more about the objects then the physics engine. You know you only need to check for inner collisions when the outer object is colliding. Hence you can probably out optimise the physics engine.

    This of course assumes that it all matters for performance. It probably doesn't. So you should choose the easiest one to code and maintain.

    Edit: And if it does matter for performance, then profile it. Don't trust random advice on the forum.
     
  8. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    This is the most appropriate answer for every post asking for "what is fastest".
     
    ericbegue and Kiwasi like this.