Construction:
Of course I started with the player tank. I am still not entirely sure if I have structured the game object of the tank in the optimal way. Later in the process I stumbled upon this Jason Weimann video (https://www.youtube.com/watch?v=UWGspLJ-_8U) and I will surely revisit the components structure of the tanks again at some point.
For now, I think the component model is decent – box game objects, which contain the elements in a small hierarchical tree (you gotta keep them separated!). The most important questions were:
– who holds the tank data structures
– how much of a separation do I need for the whole tank model and it’s turret
As this is my first experience with mouse control, the turret was a bit tricky, as it had to be controlled by the mouse, while still being part of the tank itself, which is controlled by the keyboard.
Later on, when I get to add animations, I will probably have to separate the tracks from the chasis in a separate objects, contained in another game object, in order to be able to stick an animation script to that object, which will listen for the current speed of the tank and the motion of turning around. A sync with that would be good, although it will probably be not very visible, given the position of the camera and it’s distance from the model.
Data:
The data structure itself works well and I think there will be no more big changes in that department:
– one class for the layers – each layer has only a weapon type and health.
– one class for the layer stack (TankData) – in addition to the Stack<Layer> structure it has a bunch of methods for extracting stack stats – the size, balance, top layer weapon type, etc.
– a ‘Tank’ class – this one is MonoBehaviour, as it is attacked to the tank game object. This is the main script that communicates with the turret, the mover scripts, behavior scripts, etc.
– the turret has its own scripts – a main ‘Turret’ script that for now only switches the visual model to match the weapon type of the tank; it has also one script that keeps the turret pointed to mouse cursor once I get to tackle shooting, there will be a script (or, more likely, series of scripts) to handle that – each of the weapon types will have distinctive shooting style and that will add some more structure to the data base.
= = =
Problems:
* One problem is the proper game object and component hierarchy. I mentioned that I am not sure the current GO/Component structure of the tank is perfect. I hope I will not find later that I need to make drastic changes, but this seems unlikely. Using game objects to hold collections of other objects is a fine way to approach complex models. I like to think that if you are unsure how to handle a concrete model structure, a good way to think about the model is to imagine it’s all regular C# data classes – so just follow the ‘Single responsibility principle’ from SOLID.
* The movement of the turret was a hell of a ride. And I am afraid it is not over yet. From afar it appears that it faithfully follows the mouse position, but looking closely reveals that the turret does not rotate around the proper point (the center of the turret itself) – instead it rotates around the midpoint of the whole object, which includes the length of the weapon itself. I am yet to research how to approach that. Also… there’s a design problem I haven’t tackled yet – the weapon does not tilt, which means that I can not simply shoot forwards from the respective gun barrel. For the gun and the rocket I can apply ballistic trajectory, but what about the raygun – it should be simply a raycast. If your tank is on an upwards slope (for example) and the target is on a plateau in front of you, you will simply shoot above it. For a game of this type I will obviously have to cheat in some way, but how exactly… uncertain. Should I lower/elevate the vector, depending on the circumstances? If so – should I move the visuals of the turret also? For steep angles that might look bad. If I don’t, it might look bad to have the beam shoot from the raygun ‘barrel’ at an angle. We’ll see.
* One more thing about the rotation of the turret – oh, my! There are (of course) more than one way to rotate object X towards object Y. As most of them use Quaternions and that part of math I do not wield, I had to simply try every solution I found. For the life of me I cannot tell what I used at the end without checking my own code. One of the big problems is that (because that math is not very accessible) when people look for help in StackOverflow, some good soul gives a solution (sometimes as generic code, sometimes more tailored to the specific example), but usually there is no explanation ‘why’ exactly this is a good way to implement the rotation. And inferring that from the example in question and figuring out how to translate that to your own case (if at all applicable) is… an adventure 🙂 Anyways, I’ll get to know that better in time.
Resources:
I’ll have to add a section resources at some point, but for now – here’s the one resource I managed to mention here:
Jason Weimann – “Unity3D Architecture – Where should I put my components? (and a little Weapon Swapping)”
https://www. youtube.com/watch?v=UWGspLJ-_8U
P.S. Let me say in advance that Jason Weimann is one of my primary sources. I cannot recommend his channel highly enough. The same goes for quill18creates.
#SimplyPriceless.
There are others also, but those two are my most common youtube sources and are the absolute go to!