A while ago I tried my best to write my own code for the chest in my room game to open. I tried all kinds of program blocks which kept sending errors my way. Eventually I had one which should have worked, but I thought something was missing from it as the chest still wouldn’t budge.
I had made the chest earlier in Maya, then animated the lid opening and exported it all as an FBX. The animation imported into Unity, but nothing I did could trigger it on mouse down. It would open automatically if I left it on that option, but it would have defeated the purpose of going inside to look for the key.
As I already struggle with programming, not being able to do such a simple seeming feature by myself was very frustrating. Fortunately, with Ant’s help, I got it working.
In my inspector I had these three things leading directly to the animation working. I had physics on my model so I could click on it, code embedded so I could give it a function, and the animation to link in with the code.
This is the code that eventually worked. It only played on click because it all got linked up in the animator. Ant added the ‘New State’ stage between the other two. That means it will not play until it is clicked. It turned out exactly how I wanted it to in the end.