Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Player detection by AI, which is more efficient?

Discussion in 'Scripting' started by Joe-Censored, Aug 20, 2013.

  1. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    This is a general question about which is likely more efficient. I'm building an MMO style ocean based game where individual zones will be quite large. I'm deciding on how to implement detection of hostiles (usually players) by AI controlled NPC ships. For example, when a player gets within say 2,000 units distance from an AI ship the AI will notice the player on the horizon and take an action (for example, steer to attack, ignore, or even run away).

    In other games I made long ago (not Unity) I would normally go down the list of players and calculate the distance of each player from the AI controlled ship and then have the AI make it's decision based on that. I want to in theory support several hundred players though, and potentially have that many AI controlled ships in the same zone, which is a lot of calculations to do for every AI ship.

    I was thinking though that with Unity I could create a sphere collider with a 2000 unit radius around every AI ship, and then the AI would just act on enemies after they on trigger enter the collider. If multiple hostiles are within the collider's radius I can have the AI ship make decisions on who to attack, but the list of players to act on is now pretty small.

    I'm worried though that a fast moving ship with a 4,000 unit wide collider zipping around the game might not be a normal use case for colliders, and they may not be optimized for this. Anyone have any experience with this, or is there an alternative way to handle this I'm not thinking of?

    Thanks!

    Here's a screen shot of the game if anyone is interested by the way
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I would use Phsyics.OverlapSphere

    I would only pulse it every 5 or so seconds, maybe every 10 seconds. This will reduce the load.

    I may even do it at different radius to reduce the potential contacts retrieved, only checking the next radius if nothing in the smaller returns

    ie 100, 200,400,800,1600
     
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Excellent, thanks for the fast response!
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    also, look at InvokeRepeating

    this is how you will setup you 'pulses'
     
  5. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,616
    All those spheres are likely to cost the physics system far more than a bunch of distance checks.

    You can also do all of those distance checks relatively efficiently if you do them in a separate object that checks pairs of ships, as opposed to having each ship check every other ship, and then calling a function on both if the distance is less than a given threshold.

    Still, each ship doing a few hundred distance checks every few seconds really isn't a big deal at all. To be honest, I'd personally just code this the naive way and optimise it later if performance of that turns out to be an issue, which it most likely won't.

    One more thing to consider is that if you do some simple spatial partitioning you can probably cut out 90% of the calculation based solely on where a ship is.

    By the way, the best answer to this and any other "which approach is faster" question is always "try it out on your target hardware". You could whip up a heavily loaded prototype of each case in under an hour and see which one performs better, rather than asking random people on the Internet to guess for you. And whenever someone is picking between two reasonable (ie: not stupid) options, guessing is really all we can do.
     
  6. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    True enough. Ive never actually tried to compare overlap sphere with basic distance checks, but I suspect they work much the same way anyway, and if you add layers in, there's probably no difference.


    I dont actually use overlap sphere much these days as opposed to doing simple distance checks within some kind of manager that holds references to all my objects im checking. This was a 'performance optimisation' that I never proved.


    These days I dont offer more complex solutions on scripting (such as using a manager with distance checks), as often is hard to determine if the OP knows scripting, or is just getting into it.


    Classic example was the missile tracking thread. Posted a correctly coded (albeit initially not quite right) solution, only to have the OP complain about too many errors... Worth noting, that I put the same code I posted into a JS file at home, and there were no errors... obviously it was a case of PEBKAC