Monday, 16 December 2013

Unity Gotcha - What's in a name?

I like my folder structure in any project to be tidy and understandable. 

Cosmetically I find it odd to have folders in folders with the same name.  For example a Materials folder inside another folder called Materials.

I tidied up my folders and used the name 'Resources' to put all my assets in, within that I have Materials, Textures etc.  I thought it sounded like a good name to use.

 


Bad idea!  Folders called 'Resources' have a special meaning in Unity!

Anything in a folder with that name anywhere in the project folder tree will get compiled in to the game build even if the assets within it are never used in the game!

Everything in any other folder name only gets included in the build if it is used somewhere.

Luckily I spotted this before I had gone too far and can move everything to a folder with another name, like 'Content'.

Now I know that some folders have special meanings, I will be more carefull in the future.

Sunday, 15 December 2013

Unity Code Naming Conventions

I did a quick bit of searching to find out what naming conventions were used.  It turns out that there isn't really one.

http://forum.unity3d.com/threads/135617-C-naming-conventions-for-Unity
http://answers.unity3d.com/questions/10571/where-are-the-rules-of-capitalization-documented.html

Unity supports three programming languages.  I'll ignore Boo because I know nothing about it and there are few samples.   Most of Unity's older code base is in UnityScript.  That has a very similar syntax to JavaScript.  In the UnityScript code base the only convention is that class and function names are in PascalCase and variables are in camelCase starting with a lowercase letter.

C# appears to becoming very popular with Unity developers, if not the dominant language but as the syntax is similar most people can mix and match if needed.

As C# is a Microsoft language many people have adopted the Microsoft guidelines for the use of case when  working in Unity.  I also prefer those naming conventions for case, they are:
PascalCase for public and protected properties and fields and camelCase for private properties and fields with PascalCase for all methods independent of if they are private, public or static etc.

The case is only significant for public or protected elements so that non-case sensitive languages can always reliably access them:

public Method(parameter)
protected Method(parameter)
private Method(parameter)
public Property
protected Property
private property
public Field
protected Field
private field

Some people like to use an underscore in front of _camelCase for private fields but that is their personal preference and appears to go against the recommendations in some of Microsoft's guidelines.  Again, that is only significant for public fields, what you use privately is just for readability of the code.


Saturday, 14 December 2013

Texture Quality

My first hurdle with Unity was understanding texture importing and displaying them in 2D.

Unity is very scene based.  Many games and most tutorials put all the options within the player controller of the scene.  I find this very odd.

I wanted to start with a menu, the sort of thing virtually all commercial games have and using that menu select the game options and launch the desired level.

That was not too difficult.  What has delayed me for the best part of an hour is texture quality.  I wanted a good quality logo. 



I kept getting a horrid artefact ridden blur!



Every setting I tried with the texture type set to 'Texture' resulted in the same reduced image.  It turns out that I need to use the 'GUI' texture type with a format of 'Truecolor' to avoid the texture being compressed and resized when it is imported. 


You drag the texture in to any folder, as you do with most assets and then change the settings.  Remember to press 'Apply' to see your changes.



Those settings conveniently led me to the 'GUI Texture' and 'GUI Text' objects that are probably better for me to use to create my menus.

Monday, 9 December 2013

Unity Startup

As I am new to Unity I thought it was worth noting down any observations I have as I go.  Once you become familiar with any development environment you, or at least I, tend to forget how difficult it was to find my way round when I first started.

By default Unity starts with the last project opened.

While experimenting I have found myself swapping between projects to look at different features.  It is therefore more convenient for me to select the project I want to open when I start Unity.


I want the Project Wizard to be the first screen I see.  It is easy to change in the Edit->Preferences form.


Enable 'Always Show Project Wizard.'

From then on Unity will start by asking you which project to open.

Friday, 6 December 2013

Unity 3D SVN Setup and Usage

I have just started to create projects for Unity 3D to try things out.

I use Subversion (SVN) for my code storage and wanted to do the same with my Unity projects.  Luckily I had been doing lots of reading so had already come across the fact that unlike code based projects Unity needs a few changes to support using SVN.

The changes are very minor but important.  These include some useful reminders detailing what to do:
http://docs.unity3d.com/Documentation/Manual/ExternalVersionControlSystemSupport.html
http://blog.teamthinklabs.com/index.php/2012/04/11/unity-3-5-and-svn-now-easy-and-free-for-all/

Most of the guides mention using Meta-files.  Version 4 of Unity has two settings, hidden meta files or visible meta files.  I took them to mean use 'Visible' Meta Files' for SVN.

Edit -> Project Settings -> Editor...  Mode = Visible Meta Files

There are also Unity add-ins for helping with this but having everything in one intergated development environment (IDE) is not something I have ever worried about so I did not look in to any of those add-ins.

I use TortoiseSVN, and to make things easier I use the Ignore list to stop the Library folder showing up as missing.  That folder does not need to be stored with the source code because it is a local cache and will be rebuilt as needed.



Do not select the 'Library(recursive)' option because you may want to use the name 'Library' elsewhere in your repository.

 

I use the 'Check for Modifications' feature to make sure I commit any changes I make within the project:


When using SVN with Unity do NOT rename folders either in Unity nor with SVN.  Renaming in either case messes up the other

The simple solution is to always create a new folder in Unity and move the assets from the old folder to the new folder within Unity.  You can then delete the empty folder in Unity.  Add the new folder from your SVN client and commit the project.

 


You can delete or move assets within Unity it is just renaming folders that has a problem because the meta-data for either SVN or Unity is not updated by the other.

Although it it not tidy, missing folders and files can be marked as deleted when you commit your changes to the SVN repository.

Any missing items must be marked to be included in the commit before you do an Update.  If you do an SVN update before marking the missing items then those items will be restored from the repository.


Tortoise SVN has a convenient link to mark all missing items as deleted.

There are other ways to do the same things and here is another article on the same subject.


Wednesday, 27 November 2013

Unity 3D FPS Prefabs Summary

When I started work on Diabolical: The Shooter I made a joke that it might take me ten years.  I'm not there yet but I have been going for many years and learnt a huge amount on the way.

I chose XNA because at the time it was the only way for independent developers (indie) to publish to a console.  With the advent of the Xbox One it was announced that Unity will be supported and provided to the initial crop of registered indie developers.  It is hoped that eventually everyone will be able to developer for the Xbox One but as yet no details or dates have emerged.

I have decided to do some experimentation with Unity to decide if moving development of my game over to Unity is the most likely to get the game finished to the standard I would like within my lifetime!

I will post my progress with a Unity version and take a view at some point which version I will complete.

The first step is to see what prefabs and templates are available to get me started as quickly as possible.

Although I want a Third Person view eventually I want the controls and features to behave more like a First Person Shooter so I intend to start by looking at those.  I also need multiplay, online with AI, so they are also prefabs to look for.

Any views I express about the quality or suitablity are my own opinion for my own use and may not be applicable to anyone else's use of that same asset.

Realistic FPS Prefab
+ Movement, Multi-Weapons, Pickups, Melee, Health, AI(simple)
- No Multiplayer, No Online
Full level sample and ran nicely.
Very Good Reviews.

FPS Kit | Version 2.0
+ Multiplayer(Photon), Online(Photon), Movement, Multi-Weapons, Pickups
- Bugs(claimed), No AI
Limited samples but what was there worked well.  See the forum thread for more details.
Good Reviews.

Unity Shooter Engine
+ AI, Third and First Person
- No Multiplayer, No Online
Sample did not finish loading!
Good Reviews.

Free Unity FPS Starter Template
+Online
Need to e-mail to get sample code.
Looked very limited and slow in the video.

Free FPS Kit by OneManArmy
+ Vehicles, Multi-Weapons, Pickups, Melee, Health.
- No Muliplayer, No online, No AI
I tried this out and it has everything I need as a starter and more.
Good Reviews.
The web site has other useful stuff on it.

Ultimate FPS
+ Movement, Pickups, Melee
- Just a camera
To maintain a 5 star rating with so many reviews is very good.
Very Good Reviews.
Update:  So much more has been added to this since I first looked.

Other things I found that I have not folowed up on:
http://www.youtube.com/watch?v=qdnWby6zx3s
http://www.youtube.com/watch?v=5I5GJbKj1Cg

Don't forget all the excellent complete sample projects provided by Unity themselves:
https://www.assetstore.unity3d.com/#/category/97

Saturday, 21 September 2013

Muzzle Flash

I've just been asked a question by way of a comment to this blog.  It's easier to reply by way of a new post.  I also haven't had anything to update on the blog for a while so this does two jobs in one.

State of Play

I haven't had anything to say for a while because at the moment my code is broken.  By that I mean I'm in the middle of changing something fundamental and it won't compile, let alone run.

I don't like it when the code is in this state.  I prefer to make small changes and at the end of every day the code compiles and I can test it.  For the last few weeks it has been a mess.

I am in the process of separating the client processes from the server processes and adding all the messages to communicate between the two.

I am concentrating on just the messages to get to a state where I can compile and run the game again but just to spawn a character in to the world, the state of every part of the world needs to be sent from the server to the client and the message to say what and where to spawn.

There's been a fair bit of design and redesign but now it's mainly a lot of typing.  I'm nearly there but not quite.

The Question

As mentioned, this post is to specifically answer a question.

"I've been working on a project in which you've helped me out a lot and now I'm at point at which I'm experimenting with particles. I've seen the muzzle flash screenshots on your blog and wanted to ask how did you go about positioning the exact spot to your weapon for the muzzle flash effect."
It was asked by 49ers94 from the Xbox Live developer forums.



It refers not only to the muzzle flash shown in a previous post, but to the start of the projectile particle effects shown in several of the screen shots.

The answer is simple.  I store the relative position of the muzzle in relation to the origin of the weapon.  That position is transformed by the direction the weapon is aimed in to calculate the position of the muzzle at any point in time.



 /// 
 /// Get the muzzle location in world space. 
 /// Grenades will return the hand position because the offset is zero.
 /// 
 public Vector3 MuzzleLocation()
 {
     // Vector3 MuzzleOffset - relative to the weapon origin
     // Matrix WorldPosition - position and rotation of the weapon model
     return Vector3.Transform(MuzzleOffset, WorldPosition);
 }


The above code is within the weapon model wrapper class that I use.  The weapon is already moved by whichever bone the weapon is attached to.

If you calculate that relative muzzle offset position manually for each weapon, that is all you should need to do.

One note on the particle effect.  The muzzle moves quickly as the character does but to make a nice effect the flash is not instantaneous.  I found that if the particle effect stayed still for its duration it looked odd when the weapon moved away from the still visible flash. 

The solution I used was to have very short duration particles and create new particles at the new position of the muzzle each frame.  This is not how a real flash would happen but this is what I found looked best.

Coding my own Tools

I've taken it a bit further to make it easier to create and edit more weapons.  I included a visual method to adjust the muzzle position in to the model viewer that I use for my game.



To make my life even easier I create all my weapons facing the same way and at the same scale.  I have not created all my own models but I take the model in to Blender, change its position and scale and output the model to FBX for use in my model viewer and in my game.



I save the relative muzzle position in to a text file for each weapon which is loaded in the content pipeline.

I think that covers everything on that topic.  Back to coding my client server architecture...  After an all day breakfast that is.