Wednesday, January 23, 2013

Unity 3D and Penelope Tutorial in C#

Its been a while since I posted (more than 6 months). Not much has happened as I have been out of loop on game development as I had lost some of my interest as well as important things in my day job interrupted. So recently I started to get back into game development (again!) and I was as usual toying around with Objective-C and Box2d etc. I finally had a good game concept, the most important thing in your journey.
It will be a socially responsible game with some fun attached to it. At least that's the idea. Hopefully when I have the game developed to a certain level (level 1 maybe?) I could start blogging more about the progress there.
But for now, I wanted to share my new found interest in Unity 3D as the choice game development platform moving forward in my journey. Why Unity?
Thinking about my whole adventure or rendezvous into game developement I realized I dont have any more chances. This is like my last ditch effort possibly. And lets say I get it right in Objective-C/Cocos2d framework and somehow there are tons of downloads (just being positive!). Now to publish it back on Andriod (or for that matter Windows 8) I would have to recode the whole game unless I use something like Cocos2d-X (C++ version of Cocos2d) which is still a pain.

Unity 3D

So enter Unity 3D. Once your game is created, with a click of a button you can recompile it to push to a different platform. As simple as that!

Unity uses 3 different scripting languages : JavaScript or UnityScript, C# and Boo (a never heard of or used language). You are free to pick any language and also mix and match any language in your game project. Without delving into too much details, suffice it to say that it makes your actual coding much easier. Corona SDK can also do cross platform compiling but I think I like Unity much better. The base version of Unity is free and allows you to develop as much as possible in conjuction with the Unity Remote App on Apple App Store and Google Play Store.

Another reason for me to switch over to Unity is the fact that my graphic artist (a very talented one at that www.3dtariq.com) uses this platform at his day job and I figured its easier for me to learn a new scripting language and switch over to this cross compiling platform than to try to continue with platform dependent coding.

And hence my new forays into the world of C# scripting.

Penelope Tutorial

Penelope is a fully functional game published by Unity on its asset store with all the source code and a 162 pages tutorial guide. But the code is entirely in JavaScript. So as usual, being very familiar with code conversion (courtesy my Steering Behaviors code in my past blogs), I went ahead and did a complete conversion of all the .JS files into .CS files. Its actually an old tutorial and I have found some of the code converted already on the net but this my full attempt to doing all the files as I have not seen all of them converted. Took me some time as I had to come upto speed on C# and then go over the tutorial. The tutorial is excellent but assumes you are familiar with Unity environment. Lots of code in it. But I finally finished converting every line of code and made sure it works as the .JS version. There are a few C# quirks that you need to be aware of when you convert from JavaScript or as they call it UnityScript. I am not going to detail them here but if you look at the code you will see the difference. One of the pros of coding in C# is its strongly typed nature makes it imperative on you to actually figure out what is the type of public or private variable you will be using. I learned a lot about C# and compiler errors etc while doing this exercise and that was my goal. Hope it helps somebody else. So here you go. 

Penelope C# Scripts

4 comments:

  1. Hi man!!!

    Good job!!! So, Im trying to probe this scripts but looks like doesnt work. When I probe on my nexus 7 penelope joystick dont make penelope walk, some help??

    Regards and thank you.

    ReplyDelete
    Replies
    1. Hi Cesar,
      Thanks! I have not tried it on Android. I know it works on the iOS as far as I know! The joystick.cs is a straight port of the existing joystick.js script.
      nata

      Delete
  2. This is awesome! Thanks for doing this as this will be a great tutorial to help me with learning to make games in C#. Also do you plan on maybe updating it to work for the Windows Store? I assume it will work no problem on Windows Phone 8. Anyways thanks again for converting the tutorial. 😊

    ReplyDelete
    Replies
    1. Thanks David. I dont have any plans of that as of yet on Windows 8. But I think if Unity3D gives you the option you should simply be able to change platform in build settings and publish it to Windows 8!
      nata

      Delete