Search Unity

Ways to send "damage" and "ship radar" system, optimizing CPU time

Discussion in 'Scripting' started by shivansps, Sep 24, 2015.

  1. shivansps

    shivansps

    Joined:
    Feb 26, 2014
    Posts:
    60
    Hi,
    i know this has been talked A LOT, im trying to figure out a less intensive way of transmiting damage and a radar system for ship AI, i whould like to hear your opinions.

    So what im doing right now:

    -Damage:
    Im using ridigbodys2D as triggers on the weapon "ammo", and colliders 2D on the ships, the "ammo" have a script attached to them with all the details, like who fired it and hull and shield damage, the scirpt that fire the things sets the tag of the gameobject to match the tag of the ship firing it, im using object pooling for the ammo that already helped me a lot.

    On the ships the basic script that controls damages have a "ontriggerenter2d" function, when that happens, it first compares tags of the ship recibing with the tag of the ammo, because i need to ignore "friendly fire", if it does not match them i do a GetComponent of the script the ammo has and i apply the hull and shield damage, that also allows me to know what ship is attacking it, that the AI uses.
    I tryied to avoid the GetComponent by, at the moment of firing the ammo using changing the name of the gameobject by using Damage.ToString(), and them int.Parse at the recibing end, i cant send separate hull and shield damage neither i can send the attacked data, but to my suprise itseem a little faster than doing the getcomponent.
    Not sure if there is any other way i could use?

    -Radar
    The radar system what it does right now is:

    Every 5 seconds it does a "radar sweep", the radar also have several vectors, my ships classes are assigned to different layers, ie fighters in a layer, frigates in a layer, battleships in a layer, there is a total of 4.
    So what i does is:
    Physics2D.OverlapCircleAllNonAlloc, 4 times, one of each layer and stores the results in a diferent vector, so when the AI needs a target it calls a function that retuns it depending of the class its looking for.
    But when i do that i need to sweep trought the correct vector ignoring allied ships, so tag comparing until i find a enemy ship.

    As a alternative way I was thinking about maybe saving the results of all layers in a big vector, thus reducing the number of Physics2D.OverlapCircleAllNonAlloc from 4 to 1, but them i gona need to sweep trought the vector, ignoring allies and comparing layers.

    Other way whould be after doing the Physics2D.OverlapCircleAllNonAlloc, sweep trought the vector, ignoring allies and adding each target, depending of the layer that is on, to a list, what whould also means i need to clear the list first, every 5 seconds, does not seems like a good idea.