Performance question

Updated on June 7, 2018 in General
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on June 7, 2018

Hello guys,
For example we have 50 coin in game and we want to calculate distance between each object to player. How do we get more performance? In for loop calculate distance from each object to player and attach the one object in scene or just calculate one of them and attach it each coin in scene? Which one is give more performance? The amount of coin can be increased.

Imo when calculate every object in loop it seem like overloading the code. I don’t know am I right.

  • Liked by
Reply
1 on June 7, 2018

While you can use math to calculate how big of a problem it is, you don’t need to. Your game likely doesn’t have that level of complexity, therefore I doubt your coin code is making any real impact (unless you’re making a lot of them). 

If I were you I’d make a few of the minor changes listed below and not worry about it. Checking distances shouldn’t be very performance heavy unless your code and/or PC are absolutely potato. 

Regardless, some minor improvements would include:

  • If the coins don’t move, assign their position transforms only once in Start() and don’t update them.
  • Have the player script update a public playerPosition transform variable. That way all the coins aren’t constantly accessing the player’s script, and then its transform component. Have them access its public transform variable inside the script instead.
  • Only run coin code if player is in range.
    if ( playerPositionTransform < distance )
    {
        // Run Coin Code.
    }
    

I honestly doubt it’ll make any difference, but testing the suggestions and making a few of your own improvements couldn’t hurt.

on June 7, 2018

Thank you Buldron.

Show more replies
  • Liked by
Reply
Cancel