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. Dismiss Notice

Weapon Switch performance issue

Discussion in 'Scripting' started by DevM22, Aug 15, 2021.

  1. DevM22

    DevM22

    Joined:
    Aug 10, 2019
    Posts:
    14
    Hey everyone,

    I was wondering if anyone had implemented weapon switching in a game and if you had performance issues. At the moment I am disabling the gameobject of the weapon I want to swap out and the weapon that is swapped in is set to active, it seems that the main performance issue comes from all the OnStarts of the script activation upon swap. So in these next screenshots the Shooter and AudioController scripts are both in the weapon.

    I noticed that if I spam the swap weapon I have spikes in the Profiler:
    upload_2021-8-15_15-17-40.png
    Profiler before:
    upload_2021-8-15_15-18-2.png
    Profiler after:
    upload_2021-8-15_15-18-20.png
    This is the Start of the AudioController
    Code (CSharp):
    1.     void Start()
    2.     {
    3.         gManager = GameManager.Instance;
    4.         source = GetComponent<AudioSource>();
    5.         canPlay = true;
    6.         shotgunReloader = GetComponentInParent<WeaponReloader>();
    7.     }
    This is the Start of the Shooter script:
    Code (CSharp):
    1.         pooler = ObjectPooler.Instance;
    2.         playerNetwork = GetComponentInParent<PlayerNetwork>();
    3.         isSwinging = false;
    4.         gManager = GameManager.Instance;
    Is there anything I could change?
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Hard to say if 1ms for 1 frame is really that much of a performance issue, but you could always start the game with all these scripts active and then de-active them at the end of Start() and consider it part of the loading process.
     
  3. LethalInjection

    LethalInjection

    Joined:
    Jan 25, 2015
    Posts:
    36
    You could try to move the "GetComponent" methods into Awake, otherwise, "object pooling" maybe the next step in performance increase.
     
  4. DevM22

    DevM22

    Joined:
    Aug 10, 2019
    Posts:
    14
    Thanks for the responses!

    I did what Grozzler suggested and it seems to have helped.

    Already have some object pooling going on, wanted to see what I could do increase the FPS of my game. By far the most heavy stuff is the renderPipeline, it seems that the Universal Render Pipeline Camera Stack is taking as long as 12ms to do which I'm not sure is normal.
    upload_2021-8-15_15-55-1.png
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,196
    You have to dig down further in that profiler sample. It's probably something like real time shadows or whatever, but it's hard to tell when all we're seeing is RenderSingleCamera.
     
  6. DevM22

    DevM22

    Joined:
    Aug 10, 2019
    Posts:
    14
    I am not sure why there are 5 calls, I have all the other cameras disabled apart from the main one and UI but it still seems to call them?
    Hopefully this shows more:

    upload_2021-8-15_17-45-35.png
     
  7. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    I don't see any performance issues. All of the "Self ms" times are basically 0, which is how long those specific functions take to run. You probably just have Vsync on and there's a Gfx.WaitForPresent() in there idling around the bottom of your profiler.