- Home /
How to translate a OOP class diagram to a unity game/unity code
Iv'e been coding games in c#, visual studio using OOP. Before starting a project i've always liked to draw up a class diagram to get an idea of how everything, the classes and objects are going to be connected.
And now after transitioning to Unity and scripting, i'm a bit confused to how i'm supposed to organize my scripts and connect them and the GameObjects. So i've come to understand that Unity consists of GameObjects in the scene, which consists of components, which a script is.
And lastly, i'm now starting a project on a 3D Tower Defense game and have drawn a class diagram as i would have for a Visual Studio game. How do i translate this to GameObjects, components and scripts in unity, thank you.
Would you be willing to share an excerpt from a class diagram you've made? This would make it easier to help draw correlations in built-in elements for Unity.
Unity uses C# (generally) scripts, where a script derived from $$anonymous$$onoBehaviour can be attached to a GameObject as a Component. These can be dragged and dropped onto GameObjects in your scene, or located when you choose Add Component (A script derived from $$anonymous$$onobehaviour, however, cannot be used EXCEPT by attaching it to a GameObject).
Any such Component script will be you entry point(s) to run scripts from (for example, using premade functions like Start() or Update()).
Additionally, all GameObjects are guaranteed to contain Transform data, containing position, rotation, and scale information. In following OpenGL standard (at least), every GameObject's Transform data is fundamentally a child of the Scene, and those objects can have children of their own (without meaningful limit on depth or quantity).
Anyway, those are some of the basics, but again, any direct correlations to make would need to be based around your own diagrams. Any example you could provide would allow for a more detailed and focused association.
Answer by jackmw94 · Nov 09, 2020 at 11:19 AM
For the towers:
public abstract class Tower : MonoBehaviour { }
{
[SerializeField] protected float _fireRate;
[SerializeField] protected float _damage;
}
public class MachineGun : Tower { }
public class LaserGun : Tower { }
and for the enemies:
public abstract class Enemy : MonoBehaviour
{
[SerializeField] protected float _health;
}
public class Tank : Enemy { }
public class Archer : Enemy { }
Then you can store generic tower / enemy code in those respective classes and it'll be the implementations of these classes - machinegun, lasergun, tank and archer - that you add to your game objects.
Answer by merctraider · Nov 09, 2020 at 07:45 AM
Well you didn't show the class diagram so I'll just make up an example of the easy way to do this.
For the tower AI you could make an abstract class, defining all the parameters required for a tower (e.g. fire rate, damage, etc.). Put in all the methods that you expect all towers to be doing (e.g. aiming at enemies) and define the methods that you expect each child to do differently as an abstract method (e.g. Shooting). Once you're done, just put that script as a component to the tower gameobject and save it as a prefab.
You'll also need Manager scripts and scripts that would handle the projectile of course, but I'll leave you to figure that one out.
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
3D / Top-Down - Make a script to let the player zoom the camera in and out 0 Answers
UNITY 3D: How to make the camera follow the player? Smoothly 2 Answers
How to make the camera follow and face a rolling object? 2 Answers
Multiple Cars not working 1 Answer