Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

More GameObjects or sin/cos calculations, which is faster?

Discussion in 'Scripting' started by Afropenguinn, Oct 30, 2014.

  1. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    I have around 300-500 enemies that can be active at a given time. Each enemy has needs to know the spawn point for its bullet. They also have multiple spawn points based on the type of attack. What would be faster (as in more optimized): Placing "spawn" empty children into each enemy which acts as the spawn point, or to have it calculate the spawn point for every bullet fire? I ask due to the amount of GameObjects this would create.

    EDIT: On a side question, does anyone know if a box trigger is faster that a polygon trigger when used in mass numbers?
     
  2. ColossalDuck

    ColossalDuck

    Joined:
    Jun 6, 2009
    Posts:
    3,246
    Instead of the spawn point being a gameobject, why not just store a Vector3 variable in your script? That's what the objects position is anyways.
     
  3. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I suggest you profile.
     
  4. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    The problem with a Vector3 is that it needs to be updated, meaning I have to calculate it upon a bullet firing. The advantage of using a transform is that moves with the object. I suppose I could do something like:
    Code (CSharp):
    1. spawnPoint = transform.position + spawnVector * rotation
    Could you explain? I tried googling what you mean but it gave me a large variety of results.
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,555
    hippocoder means that you run a test with each method, and see which one runs more efficiently.

    You load on the number of times you're doing this (test 1000, 5000, 100,000 enemies) and see which one runs faster.

    Unity has a profiler built in as well that makes this even easier.

    http://docs.unity3d.com/Manual/Profiler.html

    But... it is pro only.

    You can still profile without it, just not as nicely. A simple way is to just use the DateTime object to track time. Or you could even just watch your framerate and see which is better.

    There are also 3rd party options as well that range from free to 30 and 50 dollars.




    Honestly I don't see either being all that expensive.

    If it was JUST ME I'd use a vector because it has a smaller memory footprint. But because I have to design with designers in mind, I'd probably use a GameObject so that my team mates could easily just plop down a GameObject and modify it.

    Using a vector would be easy as you can just call Transform.TransformPoint to quickly transform the point to world space from local space. It's not that expensive, and you won't be doing this EVERY frame.
     
  6. Afropenguinn

    Afropenguinn

    Joined:
    May 15, 2013
    Posts:
    305
    Ah I didn't know about that function! I could use a getter/setter and put that function in the getter. Thanks for the help guys!