|
I'm sure many of you are aware of the excellent "Gang of Four" book, Design Patterns. Is there was a similar resource for Unity? I'm an experienced programmer and always find it useful to read others' solutions to problems. Now that I'm learning Unity with my boys, it would be helpful to be able to implement without having to re-invent the wheel, so to speak. If there isn't a resource available, it would be helpful for others to post their patterns. Patterns that I'd be interested in:
(comments are locked)
|
|
Each of your bullets is to me a seperate question, so please refraise it or perhaps make each a new question and go a little more in details with your actual question. As I think your question is far too complex for ONE correct answer, I will try to answer most generic possible. Use STATE-machines. Periode. How? First list the states you need for your "machine" (think of them as GEARS in a GEARBOX, only one is possible at any time pr. object)
Then a "object global variable" to keep this state inside the object. In this example I have an empty GameObject called "Game", which runs the main application.
Now I use Unity's Update per frame function, to see what should happen at any given frame.
then you use a StateChanger too: This is interesting. This would be not dissimilar to the way I programmed games years ago and indeed how I crafted Wire Whizz for the iPhone, sans any real game engine logic other than what I wrote myself. With Unity However, in my current game, I haven't done any of this - the game is running in context. I am on a menu by virtue of the fact that the menu is visible. The only state we have - this is a realtime RPG - is to say whether the game is paused behind things like the player's inventory screen. I guess our game is entirely event driven, i.e. too much damage and the player dies.
May 09 at 06:12 AM
Bovine
(comments are locked)
|
|
Your question really is too broad to be answered here. As a result, my answer is going to be a bit too long as well. Still, I understand your perspective very well and besides pointing you to the reference manual (best place for understanding unity) or online tutorials (many great ones), I will try to answer to give you the "mind picture" (aka what we used to call the paradigm) for Unity and some for game coding in general. In general game programming is like traditional simulation programming except more specific tricks of the trade. As I'm sure you know most software Design Patterns are already incorporated or used. Unity has its own way. Also, it is not restricted to games. Its strengths are the ability to organize a wide variety of asset (multi-media including 3d animated/boned models) types in 3D space. So, for someone who is already experienced in programming, there are ways to keep you from re-inventing the wheel. First of all, you should be aware the Unity is a framework that is heavily data driven. The bulk of most games is done in the IDE. Much of the association, composition that you would get in traditional programming happen through either 1) direct drag and drop of components, 2) exposure of fields (public) that auto-magically enable you to drag objects/components onto or 3) searching in your code for instances of objects and their corresponding script components through methods. That said, as a programmer, I would first explore Unity from a non-programmer's perspective. Otherwise you may lose access to much of its strengths. You don't want to make the mistake of programming "down in the trees" when you need to get a bit higher to "see the forest". :-) I like the above answer (by BerggreenDK), by the way, for how a game might be organized. Still, I would (kind of) through many of those states away (in a sense) by incorporating those concepts in their own scene. This way, the main game scene (or levels) can use a more complex state machine. For the game itself, I would also have a GameState script that can be re-used across the levels. To do this, make an Empty Object and put a GameState script in it. Then make that whole composition a prefab. As a pattern (of sorts) I see each game level as a particular kind of scene. A scene can also be a start up screen, but a level is not. A level is a mind construct (as well a particular scene.) That is, a scene is a Unity construct that is a collection of data/assets arranged in a particular way. This is canonical Unity. A scene is what you see and are working on at any particular time. It ends with ".unity" and is just one of the assets in your project. A project is pretty much just a directory, where Assets is the one sub-directory you deal with but the other important one is Library (which you don't want to treat as a temp throw away, but instead keep it around at all times). Caveat: Assets and Library sub-dirs are important, since apparently not everything in your .unity file is actually stored there, but is also stored in some weird way in the Library folder. I say this to you specifically, because coming from a traditional background you might think that the Library folder is just support dll's and forget to back it up too. Big mistake which I made many times the hard way. :-( For Turn-Based Games: To the above answer I would add player number within the player LevelRunning and LostLife states. Like:
Organization of assets is just like any other data driven solution: Use hierarchy. In Unity, this means that your project assets (see project window - not scene) mimic a good directory structure of your chosing. The project is data used across all scenes (therefore levels). Look at is a your collection to make your game. You can organize it by scene (just add a sub-directory). If you have shared assets, across levels, simply add them to some directory named /Shared. I also have a directory for Models, Sounds, Materials, Textures. You'll find that for Models, it may make sense to have their materials under them. Finally, you will want to read elsewhere (again start with manual and "getting started" areas) about the Object/Component paradigm (all object you see in your world are Object which minimally have the Transform component for example) and get into the habit of using prefabs (create prefabs the moment you have a nice collection of components in your object and may possibly want to use it in a different level, or re-use it in code, or by drag/drop). Nothing above is meant to be memorized, but just to help give you that "big overview". Hope it helps. :-)
(comments are locked)
|
|
OK, sorry to answer my own question, here's a design pattern I thought would really clever. Context-based foot sounds. In the 2d platformer tutorial, the sound played when the character walks across a section of platform is a property of the platform. That way the character doesn't need to know anything about the types of platforms and the noise they should make, all he needs to do is find the sound attached to the surface he's interacting with. I thought this was very clever and extensible way to do this. That still sounds a bit too tied together to me. I think it would be best to trigger an event every time you wanted a footstep, and the platform would respond to that event.
Nov 13 at 03:48 AM
Jessy
I dont agree with your solution, think of it if you had 2000 tiles in a map. Then you also have 2000 instances of the sound for walking, running, dying, falling, shooting, exploding etc. No attach the sounds to the object that makes the sound near the camera. Eg. if the player has 5 different sounds, then attach them to the player and play them as needed. They need to come from the player anyway.
Dec 04 at 02:23 AM
BerggreenDK
I agree with the follow-up comments, that the sounds should be attached to the player. If all your potential colliders (to player) have a layer indicating their category, then simply switch the AudioClip based on the layer.
Apr 06 at 06:37 PM
mcroswell
(comments are locked)
|
Unity Answers has moved to a new system, and some users may have trouble logging in.
was my answer usefull or do you need something more?