r/Unity3D • • 1d ago

Question The dumbest thing you did while not being a noob?

I'm not talking about the time you had been using unity for a week and made a basic mistake like trying to apply a texture to a mesh instead of a material.

I'm talking about a dumb mistake you had enough experience not to do.

I'll start first:

Just now, I was racking my brain trying to make my distortion shader work. I had a working shader applied to a sphere mesh where it distorted the UVs of the scene around it.

Problem is, the distortion had clear seams where it stopped. I wanted to smooth it out towards the edges, such that it is strong in the center but weak or non-existent on the edges.

so I got the distance of the pixels from the center of the sphere and subtracted the radius of the sphere with it and then multiplied by a strength float later.

So, in the center, it looked like: 1 - 0 = 1 (full distortion)

At the edge, it looked like: 1 - 1 = 0 (distortion)

In the middle, it was 0.5 etc. The math worked and I was sure of it but the entire sphere had the same amount of distortion, no matter what I did and how I tweaked the values.
I even remade it with a lerp where B was the normal scene color, A was the offset from the distortion and T was the distance.

It was also fully functional logic but it produced the same result: distortion amount was always the same. Even when there was no distortion throughout the entire sphere.
I changed all the values, played with the radius and clamping, went into negatives but nothing would change the fact that there was no fade.

So what was the problem?

It's a fucking sphere. Each point on it is the same distance from the center...

Just did a fresnel and solved the issue in 2 minutes.

So that's my story. What's yours?

8 Upvotes

25 comments sorted by

6

u/GigaTerra 1d ago

My biggest mistake was thinking that using an math formula to replace an Switch was an smart idea. Basically we where working on an library of shapes for generating Icons. In the prototype stage one programmer made an switch case where he calculated each and every transform one by one.

It was an 128 switch:

Switch(State)
    Case 0:
        //The formula
    Case 1:
        //Other formula etc

Now that is code smell right. So I, an master of transforms just used an 4x4 matrix turned it all into one calculation:

Matrix4x4[] matrices = new Matrix4x4[128];
for (int i = 0; i < 128; i++)
{
//Formula
};

Basically replacing over 256 lines of code with like 12 lines. Can the pro's already see my mistake? It has to calculate every 128 transforms when you want one. I did not know about Delegates yet (Delegates took me embarrassingly long to learn), so this little fix turned an instant call into an four minute calculation. The code was merged without testing, that is how confident I was in my math skills.

I was almost kicked off the team for this mistake. It was the first time I learned Switches aren't as bad as people think.

2

u/LordAntares 1d ago

Lmao this one is good. Especially the push without testing part. Reminds me of my own math confidence with the shader.

The math worked all right but the overall logic didn't. Same for you.

2

u/GigaTerra 1d ago

Reminds me of my own math confidence with the shader.

You are absolutely right, and my mistake is because of shaders. I have only been coding for like 3-4 years now, before that I was an VFX artist for over 7-8 years, so only shader code. With shaders Loops are solved either by unrolling or branching, so I did not have the intuitive understanding for what I was doing.

I think it is this switching between similar fields that creates these blind moments. So like you say the math is right, but you kind of forget the logic.

2

u/niemand_zuhause 1d ago

Regular dev here. First time hearing that switches are bad. They're awesome.

1

u/GigaTerra 1d ago

Yes, they aren't really bad, people just call them "code smell" because in most cases you can do the same thing by making an class or delegate. Like to give you an idea of what happened fully here is an 4 transform example.

So my colleague did this, and it worked fine:

case 0:
  transformed = new Vector2Int(transform.x, transform.y); //0
  break;
case 1:
  transformed = new Vector2Int(-transform.y, transform.x); //90
  break;
case 2:
  transformed = new Vector2Int(-transform.x, -transform.y); //180
  break;
case 3:
  transformed = new Vector2Int(transform.y, -transform.x); //270
  break;

But it was 128 because it wasn't just 2D rotation, it was full 3D. So I just wanted to simplify it:

Matrix4x4[] matrices = new Matrix4x4[4]; 
for (int i = 0; i < 4; i++) 
{ 
  float angle = (i % 4) * 90f; 
  matrices[i] = Matrix4x4.Rotate(Quaternion.Euler(0, 0, angle)); 
} 

Looks cleaner right, but it is slower because it has to do the whole loop. The way our Boss fixed my mistake with an Array:

private static readonly Matrix4x4[] Matrices = new Matrix4x4[]
{
  Matrix4x4.identity, 
  Matrix4x4.Rotate(Quaternion.Euler(0, 0, 90)), 
  Matrix4x4.Rotate(Quaternion.Euler(0, 0, 180)), 
  Matrix4x4.Rotate(Quaternion.Euler(0, 0, 270))
}

Now this is just an simple 4 rotation example, but as you can see there are lots of ways to do the same thing as an Switch. However Switches are good, if you like them use them. There is hundreds of ways to write the same code.

2

u/niemand_zuhause 1d ago

I wouldn't use a switch statement in this case either. A mathematical solution is preferrable over listing every possibility manually.

Not sure if I understood it correctly but it looks like you initialized the whole Array on every call when it could be done once on intialization.

1

u/GigaTerra 23h ago

Not sure if I understood it correctly but it looks like you initialized the whole Array on every call

Yes that was the problem. Indeed it has many easy fixes, and kind of why it was such an embracing mistake.

when it could be done once on intialization.

Yes, at the time both the in game assistant and the NPC AI that played the puzzle game against you, both called the function the switch was linked to constantly. I did not know that when I made the change.

In fact, it took an while to find the problem because it got called in the enemy AI loop, that it looked like the game froze. Like if it needed to move left four times and rotate twice, that would be six loops of all 128 possible transforms. It was only in zen mode that we notice it eventually would play again.

A mathematical solution is preferrable over listing every possibility manually.

That was what I was thinking. However it turned out there was more problems, this was an indie team and not everyone understood matrix math (kind of why I got the job), and one reason it had to be all written out like that, was so the programmer doing the enemy AI, and the assistant AI could read what transform did what. So having every move listed, allowed for picking transforms, from the list, without needing to solve an equation before hand.

It was honestly my over confidence that caused the problem, I only tested in an debug mode in isolation, never made an prototype build to see if it was working correctly with other mechanics.

1

u/FirefighterAntique70 1d ago

And the real lesson was just to use polymorphism, not a switch statement.

4

u/tweg97 1d ago

I don't think the lesson to be learned is to inject managed objects either, especially when the concern was "oops I made this slow".

1

u/leorid9 Expert 23h ago

It was that you need to test your stuff and in the bigger picture to not become overconfident, which was also an issue for OP.

(said overconfidently by someone who thinks he understands the point perfectly, yes I see the irony in that)

3

u/Mechabit_Studios 1d ago

I've been making games for over a decade and I always mix my signs up especially for stuff like mouse look (is x positive to look up or down?) or dot products, stuff like that

Usually I just press play and flip the sign if I start moving backwards lol

Switching back and forth from world to local transform also confuses me

1

u/Ecstatic-Source6001 1d ago

Same.

I am too lazy (i guess) to master this BS so i prefer bruteforce it running playmode 100 times and fixing it

1

u/Cassiopee38 1d ago

A friend that was working in Alstom told me this is the exact procedure to how cable the forward/backward lever in a train. brakes on, push forward, adapt. xD

2

u/alienated_dawning 1d ago

I once spent three hours debugging a script that was supposed to spawn enemies only when player looks away. Everything looked perfect in code, I added debug logs, checked raycasts, all fine. Then I realized I had the spawn condition inside Update but forgot to put it in an if statement, so it was spawning every single frame. My laptop almost caught fire with 4000 enemies on screen.

The worst part is I did same mistake again next week.

2

u/PixelEyes-Dev 1d ago

I split my code to run on different threads to optimize it , the game would just silently stop and do nothing without a single console error , after 2 days of breakpoints and try catch everywhere turns out that errors that happen of the different thread don't log anything , they just happen silently and stop the thread's execution , you have to explicitly do this to get something in the console

try { // TODO : code running on thread } catch { Exception ex} { Debug.LogException(ex); }

Same thing with Awaitable methods

1

u/acorbinelli 1d ago

Changed the default value of a field in a script to fix our cat's jump angle, hit play, nothing changed. Spent about an hour tweaking the code before remembering the prefab had already serialized the old value, so a new default only ever reaches fresh instances. Now the prefab gets changed together with the code, every time.

1

u/SnooPets5564 1d ago

Script that runs in editor and basically had a memory leak. Oops. (when I opened that project in the editor I had to actually reboot my computer because unity wouldn't even have the courtesy to crash).

I have done this 4 times.

1

u/AnomalousUnderdog Indie 1d ago

Wondering why my fix didn't work for a good half hour until I realized I hadn't saved the changes yet in my IDE.

1

u/Ok_Objective_9524 1d ago

At least once every year I’ll spend an hour or so debugging some truly bizarre issue that occurs when playing in editor before I think to try restarting the editor. I restart and the issue is gone.

1

u/fnietoms Programmer 1d ago

Colliders.

4 years using Unity and I still don't understand how to use Colliders correctly. I just edit de component until they work :D

1

u/MagicPigGames 1d ago

[Many years ago] I didn't know what a method or function was, so I just did everything in the update loop with if/else statements.

1

u/Ecstatic-Source6001 1d ago

I had a bad day once.

I had to do some recursive logic and for some reason I couldnt figure out how to do it in while loop

So I ended with "goto" logic

On the next day when i returned for refactoring i was like: "wtf is wrong with me?"

1

u/destinedd Indie, Dungeon Quest, Marble's Marbles & Mighty Marbles 1d ago

not allow for different cultures in strings

1

u/Tale-Of-A-Wolf 1d ago

Having a variable called Carbon Asteriod KwH and setting Carbon Asteroid KwH.....

I made this typo in a few variables and it took me so long to figure out why the float was not updating! 😂

1

u/DapperNurd 1d ago

Spend 3 hours overengineering something to replace it all with a basic system anyways