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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How would you reduce lag in RTS game?

Discussion in 'Scripting' started by asperatology, Nov 21, 2015.

  1. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    In my RTS game prototype, I could not avoid extreme lag when there's only 20 units in a small map.



    As you can see, 1 unit has 2 triggers and 1 collider, and they are, respectively: Line of Sight, Attack Range, and RigidBody.

    The game in the editor is running 2 FPS on a gaming laptop. And it's really pushing my laptop fans.

    I would like to know what is the best way to reduce lag. Does that mean I have to remove my triggers?
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,985
    Check your profiler. What you described should do way better than 2fps. There is probably something else causing the problem.
     
    Kiwasi likes this.
  3. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Ok, I found out I'm calling Physics.OnTriggerStay() a lot. 100x a lot. It's bringing down the CPU, utilizing up to 98% of it.

    The most weirdest thing is that without OnTriggerStay(), the units themselves couldn't identify if the unit in the triggers is an enemy or a friend.

    At least I found my culprits. Thanks.
     
    Kiwasi likes this.
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Yup. Don't use the built in physics for an RTS. And don't use OnXXXStay for anything.
     
  5. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    How would I create an alternate?
     
  6. novashot

    novashot

    Joined:
    Dec 12, 2009
    Posts:
    373
    Usually you'd use ontriggerenter and exit... if you need to do something as they are in the trigger use enter/exit to set a bool so they can determine if they need to do something special. But if it is for something such as enemies in range... on enter add them to a list and use the list to find closest or whatever.
     
    Kiwasi likes this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    How many units do you want? Agent based simulations in unity tend to break down somewhere between 200-1000 agents, depending on the device and agent complexity.

    For small level simulations simply using OnTriggerEnter and OnTriggerExit will work. I would also suggest making the rigidbodys kinematic and using navmesh agents..

    For higher numbers of units you want to start using your own solution for storing their location. Something like a quad tree. Start treating Unity as a rendering engine only.

    For über high numbers you'll want to leave behind the GameObject altogether. You'll need to start using imposters. And your simulation will run entirely outside of Unity. There are techniques specifically designed for high numbers of units.