Search Unity

Check Distance from many many Objects && High Performance in C# ;)

Discussion in 'Scripting' started by Shiikarii, Dec 18, 2014.

  1. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    Hi, my name is Alex..
    and i want to make a game with huge size, that means many GameObjects and High Performance!

    Unbenannt.PNG

    This is my Basic AI Script.
    • The AI is allowed to Serach for a RandomPosition and move to that Position
    • If it arrived, the AI is searching for another RandomPosition
    • The next thing is to make it possible that every Ship has got his own List or Array with all other AI Gameobjects in it.
    • And the last thing to do, is to check if any of this Gameobjects are in a static distance to the other Objects and then it take this Objects and do it in another List or Array.
    Unbenannt3.PNG
    Unbenannt4.PNG

    And that's my Question:
    "How can i do this or check this GameObjects with the distance or with Collider Trigger".
    And my other Question: "How can i make this with High Performance.... to see my Performance with this Script, check out my Pics below".

    Unbenannt2.PNG


    Thanks for helping me! ;)
    - Alex
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I would give them a tag instead of searching for all game objects. The other thing you can do is wait quite a while to check distances in a coroutine. They aren't going to be moving that fast. If you use FindObjectsWithTag, it will return an array I believe, so then you can check their distances and throw out the ones out of range. Another thing would be to make a huge collider and have it follow the player and use it as a trigger. Anything out of range should be set inactive so it doesn't waste cpu.
     
  3. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    Yes i could check it with a Tag, that's the simplest way to do it... but you dont get performance when you search objects with tag...
    The other thing you say'd is to wait a while in the coroutine... the problem with that is, when i'm spawning a new object the object must scan all other objects to see the others distance, because other object would be destroyed and i dont wanna have them in my List :p

    the last thing you saying dosn't make sense because every little ship in my world should have a CircleCollider Triggerd, the physics engine from unity would overflow :s

    thanks for helping me... do you have any other ideas for me :/
     
  4. Random_Civilian

    Random_Civilian

    Joined:
    Nov 5, 2014
    Posts:
    55
    I'll give it a shot...
    One thing I'm seeing here is that every x amount of time, each script is searching through all the objects for specific ones which I imagine would take longer the more objects there are. Why not have a script on an empty object that holds the lists you want objects to be sorted into and add or remove objects from them when each is instantiated, enabled, destroyed, or disabled. Just store a reference to that script and iterate through the desired lists instead.
     
  5. Shiikarii

    Shiikarii

    Joined:
    Feb 6, 2014
    Posts:
    89
    Fantastic :D
    i will program this and put it in my public Manager Script!

    ohh man its so easy ^^
    should not program at 5am :D

    but i have another question: "why does that not work... see code below", maybe you can help me ^^

    Code (CSharp):
    1. void OnBecameInvisible()
    2. {
    3.    gameObject.GetComponentInChildren<SpriteRenderer>().enabled = false;
    4. }
    5.  
    6. void OnBecameVisible()
    7. {
    8.    gameObject.GetComponentInChildren<SpriteRenderer>().enabled = true;
    9. }
    when i start the game zoom the camera in and out, all child objects spriterenderer has got disabled, but not enabled.... why ? :s
     
  6. Random_Civilian

    Random_Civilian

    Joined:
    Nov 5, 2014
    Posts:
    55
    I would assume it is because OnBecameVisible was not called (add a debug statement). According to Docs it only is called when a renderer becomes visible, so since the renderer is disabled (still invisible) it is not being called... Just a guess though

    EDIT: Maye http://answers.unity3d.com/questions/8003/how-can-i-know-if-a-gameobject-is-seen-by-a-partic.html would help?

    EDIT2: Or maybe this: http://forum.unity3d.com/threads/onbecamevisible-not-working.23189/
     
    Last edited: Dec 18, 2014