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. Dismiss Notice

What is the most optimized way to detect enemy in tower defense game?

Discussion in 'Scripting' started by Le_Tai, Apr 6, 2016.

  1. Le_Tai

    Le_Tai

    Joined:
    Jun 20, 2014
    Posts:
    430
    I think I should avoid using physic and use squared distance compare instead, but I think there must be a way to optimize it further. Let say I have 15 tower and 200 enemy, it will be 3000 calculation per frame, it gonna be less after some towers lock onto targets, but I still want to push the performance as high as possible, since I'm developing for mobile. Anyone have a suggestion?
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
    The physics engine is pretty robust, and could probably handle that many calculations without breaking a sweat. But, you raise a good point, that a purely physics-based game doesn't scale as well as a manual solution would. So, consider an octree or other space partitioning scheme to reduce the number of collision checks that you will have to perform. Also, recognize that collision detection is highly parallelizable, so it can be offloaded to multiple threads or even a Compute shader.
     
    Kiwasi and Le_Tai like this.
  3. Le_Tai

    Le_Tai

    Joined:
    Jun 20, 2014
    Posts:
    430
    Thanks for your reply, I will research those
     
  4. Ckboyd89

    Ckboyd89

    Joined:
    Dec 2, 2012
    Posts:
    9
    You can also utilize Physics.OverlapSphere on each tower, based on range, and then calculate your targeting from the array that is generated. This may reduce the overall process of calculating each tower's target from all enemies in the scene.
     
    Kiwasi likes this.
  5. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    That's considerable part of the optimization in the locking to targets. Even if it doesn't find a target, it maybe shouldn't be seeking every frame. Check on every 3rd frame or so?
    CheckFrame %= 3;
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Scott2112 and Suddoha like this.
  7. Le_Tai

    Le_Tai

    Joined:
    Jun 20, 2014
    Posts:
    430
    I know how to do it the physics way. The thing is I thought the physic engine have to compare distance anyways, so I could have more control doing it my way. However after research I found that Unity physic utilize octree and probably some more ways to optimize the detection process, so I thought maybe I should just use the physic engine for detection.

    I believe doing OnTrigger events will be more efficient than OverlapSpere. And checking every n frames shouldn't improve the user experience at all if the lag is perceivable
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    $%*^$ believing. If its as critical as you say, run it through a profiler on the target device.
     
  9. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    It depends muchly if you have rigidbodies in the scene, if you do then yeah the collisions are checked even if you don't OnTrigger them. Setting collision layers can also help there, for example setting that enemy collision layer doesn't collide with itself, or turrets don't collide with turrets. But all said, smartphones have pretty good processing power nowadays, it's hard to make them lag with a game that runs smoothly on PC.
     
  10. Le_Tai

    Le_Tai

    Joined:
    Jun 20, 2014
    Posts:
    430
    That is exactly my problem: I targeting my country where a lot of people use low-end smartphone, but I don't have one of those so I posted here to see if anyone did it before could give me an advice.

    But you got the point, I probably getting paranoid about this.
     
  11. Deleted User

    Deleted User

    Guest

    InvokeRepeating("your_Method", 0f, towerFireRate)