Client framerate grinds to a halt after only spawning small number of objects.

Hello all,

As the title says, I’m having problems with client-side framerates dropping to unresponsive levels (like 1 frame every 2 - 3 seconds) after only a small number of objects are spawned.

I’m new to networking, so I’m keeping my project relatively simple.

I’m using the built in NetworkManager and NetworkManagerHUD.

The game is 2D and consists of each player flying a space ship (no backgrounds to sync). Then, they can fly around and shoot at each other. The ships and bullets each have a NetworkIdentity and NetworkTransform and are registered with the NetworkManager.

The game spawns the players at a random location, and when a player shoots, it creates a bullet on the server and spawns them on the clients. The bullets then just continue in a straight line until they hit someone, or travel x amount of distance (in which case they’re destroyed). Very simple, basic setup.

However, after either player shoots more than 5 - 10 bullets, the game grinds to an unusable state on the client side (the host’s game is still a smooth 60fps). As the bullets are slowly Destroyed, the client’s framerate goes back up. Otherwise, I have it where both players can fly around and shoot at each other, as long as there aren’t any more than a handful of bullets spawned at a time.

I could see if this happened after hundreds of bullets were spawned, but 5 - 10? Surely Unity should be able to coordinate more than 10 objects between the host and client. What could I be doing wrong?

This is both when I test it locally on my machine (running two copies of the game and connecting through localhost) as well as connecting to another PC over Unity’s cloud with the matchmaker.

Be sure to let me know if you need specifics/settings/code.
Thanks for any help, guidance, etc.

There is a Profiler that lets you know what % of CPU usage is going where. It’s a pretty helpful starting point (but not always accurate in my experience).

That is awesome. I did not know the Profiler existed. It pointed me exactly where I needed to go.

The bullet has a FixedUpdate that does some calculations (such as how far it’s traveled since it was fired), as well as recorrecting the velocity and direction, should it “bump” something that isn’t a target.

On the host, firing 200 bullets raises the CPU usage of Bullet:FixedUpdate() to around 2 - 3%. Barely anything.
However, on the client, firing 5 - 10 bullets raises the CPU usage of Bullet:FixedUpdate() to around 75 - 85%.

void FixedUpdate () {

        //originPos is where the bullet was fired from.
        float dist = Vector2.Distance (transform.position, originPos);        

        if (dist > maxTravelDist) {
            Destroy (gameObject);
        }

        if (rigidBody.velocity.magnitude < bulletSpeed || transform.eulerAngles.z != bulletFiredAngle) {
            setVelocity();
        }
    }

    [Server]
    void setVelocity() {
        rigidBody.velocity = new Vector2 (
            -Mathf.Sin (bulletFiredAngle) * bulletSpeed,
            Mathf.Cos (bulletFiredAngle) * bulletSpeed
        );
    }

After commenting FixedUpdate() out, I could shoot about a hundred bullets before I noticed the framerate on the client drop down to around 30.

Thanks again! Now I know what to look into.
And thanks for the tip on the Profiler, that’ll be a great asset in the future.