How Do I Unlock This?!

No Caption Provided

This is a combination lock, and it took me about 2 weeks straight to finally program it right. To spend so much time on one item was really a buzz kill, considering this is such an obvious real world object. But I think that was my mistake, I took for granted an item without properly researching the mechanics behind it.

I mean the lock is simple, you turn it to the number you want then you reverse your turn to the next number. And with that knowledge I went ahead and got started programming, a day later the code was a mess. Sure it spun around but that didn’t help me at all. So back to the drawing board with the thought of I really need to study this more. First break through was finding what number you are on, and it was quite simple. A circle has 360 degrees on it, the dial has 100(99 really), so dividing the degree of the y axis by 3.6 gave me the exact number the dial was on. Sprinkle in some rounding and margin of error calculations and boom done.

Second problem is having the dial know what direction it is spinning and know when the user changes direction. Now solving this just at face value was not very hard. Just keep track of a delta on the change in degree you are at and if it is positive you’re going right and negative if left. I did have to write a special case for the 100/0 number as it then jumps down 100 points. But then looking deeper into the puzzle is that how do I set the users input. When you change directions right? Well right. But then I just lost the number I was on before. The solution is just a number that keeps getting pushed up. So you can keep turning right, let go, turn right again, and the right number is still going up. When you turn left it starts to modify the left number. Now on the third number you cannot push it, it has to be in one swipe, want to make that better but this is a pretty small part of the game so I cannot take too much time on it.

The hardest part was getting the user input. I was able to get the delta of the mouse, but that only worked on one axis, and I really wanted to make it as you were in real life, a twisting motion. So I knew what was coming, trig. This was really frustrating because on paper I can do it, I can get the delta angle between two points, these points make triangles from the center of the dial. But now how do I translate that to c#? I spent a few days on this then had to concede defeat, and ask unity answers. It can sometimes be hard to swallow that you need help but after thinking about, I am wasting my boss’s money by trying to be arrogant and do it myself even though I was failing. Time is money. Thankfully I felt pretty good with the discussion on the answers site as there was no solution helped me, but each had something that made it closer to the solution. So combining all these solutions and adding some of my flavor I finally was able to produce functions that gave me the circular motions I was looking for.

The moral of this blog really is two things. First don’t take for granted the objects you use all the time or you think are trivial. There are a lot of complex mechanics behind the scenes that you need to think about before you start programming that system. And don’t be afraid to go seeking for help. You learn something new every day but the only way to learn is to admit there is a hole in your knowledge.

Obviously I can’t just post the whole script publicly but I do feel people should have this circular motion script so here it is. This is a partial script but anyone familiar with unity will get it.

Since this game is getting closer to release I am not sure if I will make another educational blog about it, but I do want to talk about it. So if you’re interested in this follow me on gb as obviously don’t want to post anything with a promo focus on the forums.

Start the Conversation