#Profiling 101

1 messages · Page 1 of 1 (latest)

nimble flume
#

What's wrong then?

faint linden
nimble flume
#

You should get something like this

nimble flume
faint linden
#

the game is going rn

nimble flume
#

Stop it

#

Click a frame

faint linden
nimble flume
#

Set that to hierarchy

#

Nevermind

#

Click on that peak

#

Then set to hierarchy

faint linden
nimble flume
#

So your game is actually almost doing nothing

faint linden
#

yeah its on a menu screen

nimble flume
#

...

faint linden
#

...

nimble flume
#

.......

faint linden
#

.......

nimble flume
#

Your a potato head. Of course we need to profile the actual game with all the enemies.

#

Smart ass 😛

#

Profiling the actual main menu

#

That was funny

faint linden
#

ok gimme a few mins to get to the point with all the enemies

nimble flume
#

Profiling 101

faint linden
#

@nimble flume

nimble flume
#

Your still playing

#

Anyhow, this looks more like it

#

Open up the PlayerLoop, and go down there

#

Look up the methods that are taking most of the 99% of your time

faint linden
nimble flume
#

Why is it still purple?

faint linden
#

i paused the game instead of stopping it

nimble flume
#

Ahh

#

Okay

#

Anyhow, theres your problem

faint linden
#

cpu rendering

#

I NEED RAY TRACING

nimble flume
#

Your physics system wrecks your CPU

faint linden
#

why

nimble flume
#

I don't know how you set it up. But there's your problem

faint linden
#

how fix

#

gpu?

nimble flume
#

You can't do physics on the GPU.

#

I don't even know why you need physics for a bullet hell game like that.

#

Normally just colliders would be enough

faint linden
#

so how fix

nimble flume
#

🤷‍♂️

#

What kind of physics are you even using?

faint linden
#

none

nimble flume
#

The profiler says otherwise ^-^

#

You are spending 200 ms on physics each frame

rapid ore
#

so on the bullets
is there a collider2D ?

faint linden
#

yes

rapid ore
#

and a rigidbody?

faint linden
#

yes

rapid ore
#

i would only put a rigidbody on the object that bullets can hit
like your player

faint linden
#

bullets dont hit the player

rapid ore
#

ok i meant the enemy in this case

faint linden
#

why would that make it better

rapid ore
#

rigidbody are expensive
having them one thousends of objects .... kind of a issue

faint linden
#

whats a lot on the screen are the rats

#

and the rats dont have any rgid bodies

rapid ore
#

so only the bullets

faint linden
#

there is no bullets on the screen

#

there is just a couple saw blades

nimble flume
#

Or skip them altogether and raycast yourself?

#

That's how I would do it in DOTS

#

But don't know if that works in MonoBehaviour style

faint linden
#

i thought unity was more powerful than this

rapid ore
#

we are still taking about a scenario like this?`

faint linden
#

yes

nimble flume
#

And do the bullets ignore collisions with bullets?

faint linden
#

yes

nimble flume
#

With the ignore layer thing right? not in your code?

#

Otherwise it's colliding constantly with hundreds of its own bullets, and the enemies too.

faint linden
#
private void OnTriggerEnter2D(Collider2D collision) {
        if (collision.gameObject.tag == "Enemy") {
            gameController.score += 1;
            GameObject xpOrb = Instantiate(xpPrefab);
            xpOrb.transform.position = collision.gameObject.transform.position;
            gameController.xpOrbs.Add(xpOrb);
            Destroy(collision.gameObject);
        }
    }```
nimble flume
faint linden
#

thats what i have on the saw blades and bullets

nimble flume
#

Yeah but then when it enters another bullet, it also fires of that code, checks if it's not an enemy and returns.

#

That will eat your CPU up too

#

Just let the bullet layer ignore bullets

#

And the enemies layer ignore enemies

faint linden
#

but doesnt it say 99% is phyics engine

#

not code

nimble flume
#

Yeah, but the physics engine calls those methods

faint linden
#

also there is still no bullets on the screen

#

idk why we are talking about bullets

nimble flume
#

And the enemies layer ignore enemies

faint linden
#

the enemies dont have rigid bodies

nimble flume
#

Okay, so you don't have anything with Physics in your game then if you don't fire a bullet?

faint linden
#

just the saw blades

#

and i dont think they are causing the latency because it was going to 0 fps even before i had the saw blades

#

here is da rat

nimble flume
#

Show the Rat script

faint linden
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RatScript : MonoBehaviour {
    public Rat ratPrefab;
    public List<Rat> rats;
    float timer;
    public GameObject characterObject;
    public const float ratSpeed = 0.5f;
    private float spawnDelay = 2.5f;
    void Update() {
        timer += Time.deltaTime;
        if (timer >= spawnDelay) {
            timer -= (spawnDelay);
            spawnDelay *= 0.99f;
            Rat instantiatedRatLeft = Instantiate(ratPrefab);
            instantiatedRatLeft.transform.position = new Vector2(Random.Range(-20, -11), Random.Range(-11, 11));
            rats.Add(instantiatedRatLeft);
            Rat instantiatedRatRight = Instantiate(ratPrefab);
            instantiatedRatRight.transform.position = new Vector2(Random.Range(11, 20), Random.Range(-11, 11));
            rats.Add(instantiatedRatRight);
        }
        foreach (Rat rat in rats) {
            if (rat != null) {
                float dx = characterObject.transform.position.x - rat.gameObject.transform.position.x;
                float dy = characterObject.transform.position.y + 0.8f - rat.gameObject.transform.position.y;
                float t = Mathf.Rad2Deg * Mathf.Atan(dy / dx);
                if (dx < 0) {
                    t += 180;
                }
                rat.vx = ratSpeed * Mathf.Cos(Mathf.Deg2Rad * t);
                rat.vy = ratSpeed * Mathf.Sin(Mathf.Deg2Rad * t);
                rat.transform.Translate(new Vector2(rat.vx * Time.deltaTime, rat.vy * Time.deltaTime));
            }
        }
    }
}
nimble flume
#

That's not this one?

faint linden
nimble flume
#

Okay so that's empty

faint linden
#

ye

nimble flume
#

I would delete it, that's still eating CPU

#

But it's not your Physics problem

#

I have no idea, you are not giving me anything related to Physics, but it's the Physics system that's killing your game.

faint linden
#

idk what to show u

nimble flume
#

Yeah, and I sadly can not help then

faint linden
#

does the profiler show anything else?

#

that will show where it is

nimble flume
#

You could open it up more

rapid ore
#

maybe 2D multi threaded could help

nimble flume
faint linden
#

but this isnt even a big game

nimble flume
#

Open up those

faint linden
rapid ore
nimble flume
#

That's the one I'm reading ^-^

#

So, circle collider is best

faint linden
#

i just dont understand why unity is struggling this hard, i switched to unity cause i was told it was more powerful

nimble flume
#

And it seems it's still looking for collisions against other enemies, as an enemy.

#

So do the layer thing I mentioned

faint linden
#

on the rat?

nimble flume
#

Yeah start with that first.

faint linden
#

how i do it

nimble flume
#

Also make sure the rat has the Layer Rat

#

It's struggling because you tell it to look for collisions against hundreds of gameobjects with hundreds of gameobjects each frame.

#

You can shoot 14 million bullets in Unity without lag, if you just optimised it.

rapid ore
#

can you try enabeling this checkbox in the unity 2D pysics settings?

faint linden
#

ok

#

mmm where are the settings

rapid ore
#

Edit -> Project Settings -> Physics 2D

faint linden
#

ok its on

#

how do i do the layer ignore thingy

#

i still dont know why the rats would be colliding with eachother if they dont have rigid bodies

nimble flume
#

That's fair, I don't know either, but I am concluding 3 things:
The physics system is killing your game
You don't have any gameobjects in your game with a rigidbody
Therefore the only thing that can still cause FindNewContacts physics checks, are the rats with the colliders.

#

And I don't know the internal workings of a collider without a RigidBody, does the collider call that physics function, or the RigidBody?

faint linden
#

the rigidbody does

#

maybe idk

nimble flume
#

If that's true, your whole story makes no sense.

faint linden
#

how do i do the layer thing

nimble flume
#

Then there are rigidbodies somewhere

#

Add a Layer Rat, set the prefabs to Layer Rat, and ignore matrix Rat x Rat

#

Or make it Enemies

#

More generic

faint linden
#

where is ignore

nimble flume
#

In the Unity menu bar, go to Edit > Project Settings, then select the Physics category to open the Physics window.

rapid ore
faint linden
nimble flume
#

Meow

#

But your still playing

#

Don't know if that resets

faint linden
#

ill restart game

nimble flume
#

Yeah, some stuff resets while playing

faint linden
#

yeah u right

#

ill redo it

rapid ore
#

il be intrested if the Multithreading is helping

nimble flume
#

Going to take 10 minutes before hes at the lot's of enemies again

rapid ore
#

might be worth doing some test setup that allows you to skip to that palce

nimble flume
#

Totally agree

faint linden
nimble flume
faint linden
#

unity bad

nimble flume
#

Not at all, like I said, you can make a bullet hell game with millions of bullets in Unity

#

This setup either does not work, or there is still something calling the physics system that you don't know of

faint linden
#

the middle chick has a rigid body but its only 1

nimble flume
#

Anyhow, you can try to make this in Unreal, see if it goes better

faint linden
#

i doubt it will

nimble flume
#

Or Godot

faint linden
#

im just kinda dissapointed

nimble flume
#

That's more inclined to 2D

faint linden
#

how do games like factorio exist with millions of entities

#

when unity cant even do like 200

nimble flume
#

You are constantly making that claim, it's not true.

rapid ore
#

have fun with that game in another game engine and your list of bad game engines will grow long fast

nimble flume
#

That's way more heavy then the stuff you are currently doing. It's also a few thousand times more optimised

#

Don't say that unity can't handle 200 entities, it's simply not true

faint linden
#

so why cant it do mine

#

they are 2d and tiny and very simple

nimble flume
#

It's also a few thousand times more optimised

faint linden
#

how is it optimized

#

i have a thought

nimble flume
#

Well that's a really long story for this example and doesn't really matter for your physics problem.
But I have spend many hours looking through the profiler and optimising everything about this system. This was the best I could get out of it.
So you can do that too, just figure out what's causing your Physics calls. Disable the colliders from the enemies/your saw blades/your rigidbodies/w/e. Just look at it.
There is nothing I can do to help anymore. Good luck with it.

faint linden
#

my rats are being put on a spawn timer so the spawn timer continually decreases

#

what happens when the spawn timer is 0 or negative and its trying to spawn rats every frame

nimble flume
#

It's your code, you tell me

faint linden
#

what

nimble flume
#

I have no idea how you made your conditional check

faint linden
#

for what

nimble flume
#

The spawn timer...

faint linden
#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RatScript : MonoBehaviour {
    public Rat ratPrefab;
    public List<Rat> rats;
    float timer;
    public GameObject characterObject;
    public const float ratSpeed = 0.5f;
    private float spawnDelay = 2.5f;
    void Update() {
        timer += Time.deltaTime;
        if (timer >= spawnDelay) {
            timer -= (spawnDelay);
            spawnDelay *= 0.99f;
            Rat instantiatedRatLeft = Instantiate(ratPrefab);
            instantiatedRatLeft.transform.position = new Vector2(Random.Range(-20, -11), Random.Range(-11, 11));
            rats.Add(instantiatedRatLeft);
            Rat instantiatedRatRight = Instantiate(ratPrefab);
            instantiatedRatRight.transform.position = new Vector2(Random.Range(11, 20), Random.Range(-11, 11));
            rats.Add(instantiatedRatRight);
        }
        foreach (Rat rat in rats) {
            if (rat != null) {
                float dx = characterObject.transform.position.x - rat.gameObject.transform.position.x;
                float dy = characterObject.transform.position.y + 0.8f - rat.gameObject.transform.position.y;
                float t = Mathf.Rad2Deg * Mathf.Atan(dy / dx);
                if (dx < 0) {
                    t += 180;
                }
                rat.vx = ratSpeed * Mathf.Cos(Mathf.Deg2Rad * t);
                rat.vy = ratSpeed * Mathf.Sin(Mathf.Deg2Rad * t);
                rat.transform.Translate(new Vector2(rat.vx * Time.deltaTime, rat.vy * Time.deltaTime));
            }
        }
    }
}
nimble flume
#

So it spawns every frame then?

faint linden
#

eh i guess it wont ever actually be 0 or negative

#

but it might get close

nimble flume
#

I'm going to bed, cya around 👋

faint linden
#

so is that whats causing it?

nimble flume
#

This isn't a physics thing is it?

faint linden
#

no

nimble flume
#

And we concluded that your problem was a physics thing

faint linden
#

but we already established we cant find anything physics related

nimble flume
#

And your profiler said it is

#

So you are wrong, you just don't know why your wrong

faint linden
#

yes

#

the fps drops from 500 to 6

nimble flume
#

I understand what's happening, that's not the issue. You still haven't said to me what's making all those physics calls. You need to figure that out. I already gave you a few ideas how, good luck with it, I'm going to bed

faint linden
#

gn ty for the information

#

for anyone else seeing this post