Making my first game – Part 3: Problems
When making my first game I encountered two major problems, which took me quite some time to figure out. Below you will find the detailed descriptions as well as solutions which I used to solve them:
Cannot interact with objects (doors/chests)
At certain, seemingly random times, interactable objects stopped responding. When I pressed the E key used for interaction in my game, they would activate or not. For a while I could not determine why and when it happened. Then I noticed that objects stopped responding when I was standing still with my character. This was new to me and I had to do a bit of research to see what was behind this behaviour.
What caused those problems? Colliders, physics settings for the project or rigidbodies? I went on a documentation hunt and read everything from the Unity manual and I finally found the relevant info. It appears that each Rigidbody in Unity may be excluded from the simulations when it is not moving, see this article for reference.
Of course the problem appears for objects that may be operated when they are stationary. So for example we have doors – they are stationary, and thus asleep for the most part. They only “wake up” when the player’s collider touches them, but after a few seconds they go to sleep again, even if the colliders are still in contact! Imagine this situation: you are standing next to a door, then something distracts you for a few seconds and then you try to open the door. The door is already “asleep” so you need to move your character a bit to trigger the collision again and awaken the door’s rigidbody.
Each hit of the player has different effect
Basically, after each hit I noticed the player’s attacked behaved differently. I would hit a barrel, and then when I tried to hit another barrely, the attack range would be smaller or I would not even be able to hit anything.
This had caused me some serious mental fatigue, especially due to the fact that I felt quite excited by finishing the attack system. I did not expect this kind of behaviour and I simply switched off Unity for that day to relax. The next day I came back to face those problems. First I checked the code, to see whether the collider does not “disappear” somewhere. Then I inspected the method of activation and inactivation of the collider. And I still was not sure what was going on. Then, during one of the many tests I switched from the Game view to the Scene view while playing the game (for whatever reason I do not remember). And I suddenly noticed, that I have a preview of colliders in this view. This was something I should have done at the beginning.
It became clear, that the collider collides (duh!) with other colliders and is itself influenced by them. Each collision would alter its rotation in a small or significant way. A few collisions would totally displace the collider in a permanent way. The animation still managed the collider’s position, so it remained intact. I just needed to reset the rotation after each animation. To solve the issue I wrote a small method and called it at the end of animation, as seen in the screenshot below:
These were the two main issues during the development of this game demo. Other aspects went incredibly smoothly and apart from some fine tuning I had little problems along the way.