Home > computer, Game, interaction design, Unity3D > Unity 3D: Part 3 – basic scripting w/example and source

Unity 3D: Part 3 – basic scripting w/example and source

I buckled down and bought a copy of Unity3d last week. They recently came out with version 2.5.1 which has significant stability fixes that I was a little worried about before. I can say after working with it for about 30 hours, it hasn’t crashed on me even once (Unity 2.5 crashed about once a hour).

The model I creating in the last part had to be scrapped since it had some problems with how I modeled it (scaling in Object Mode is a bad idea). Instead of spending a lot of time modeling again, I created a simple box man model and created a few animations just to get it working right. It imported alright and the animations work fine with it.

I added the default” FPS Walker” script to the model and was disappointed with the results. It moves, but it can’t rotate with the movement. The animations aren’t linked to the movement either, so nothing was getting animated – just my box man hovering along the ground.

Where to find scripting references

I went through the scripting tutorial they had, but it didn’t really get into much depth with the things I wanted to do with the animation – it is a good read though for a newb like me.

there are no books on Unity that I know of, so all I looked at the tutorials, scripting overview, and the reference manual.

Unity also has an offline appendix of its components as well that you can get to. I usually select a keyword in the script editor and hit F1. It will bring up the component and give a little explanation. Big help sometimes.

Coding is how you get everything to work, so it seemed the best way to progress was the 3d platform tutorial. It says it is a tutorial, but it is really more like a small book. With over 120 pages, I am finding out that there is a lot more than what appears at the surface. I got through about 60% of the tutorial, then I found a great reference on character animation and I changed course (I am more interested in that right now anyway). After reading that, I got excited and went back to the 3D platform tutorial to steal and analyze some code.

“Borrowing” Unity tutorial code

95% of the code in the 3d Platform tutorial is already done before you start, so the tutorial is a matter of explaining broad concepts and linking game objects with scripts. The default scripts that come with Unity really can’t do that much, but using scripts from the Unity tutorials as a base can get you quite a bit farther.

So I just grabbed some of the movement scripts from the tutorial and changed them a little bit (looking at the scripting references constantly). There is a lot of extra scripting for things I didn’t implement like wall jumping, but I decided against deleting it for now. Here is a little demo of box man idling, walking, running, and jumping on a plane. I also attached my source unity project along with the .blend file for the model/animation. Hope this helps on the journey to Unity mastery!

Go box man! Go! Don’t fall off the edge!

Controls:

  • Move – W,A,S,D
  • Jump – spacebar
    Run – left shift

all of the project source files

Look at my other parts if interested:

FYI – WordPress plugin to show unity files

Now that I am more familiar with the workflow of the entire process, I can focus more on functionality and the “cooler” things.

I want to add some Additive animations so the character can punch and kick while he is running and walking. Maybe some particle trails when he runs. If I get those things figured out fast enough, I will start modeling and animating a higher res character that looks cooler. Does anyone think this is helpful?

Share and Enjoy:
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Twitter
  • Google Bookmarks
  • PDF

computer, Game, interaction design, Unity3D

  1. July 25th, 2009 at 14:06 | #1

    Good stuff. Thanks for sharing the code with us. Have you checked out will goldstones full length tutorial at learnmesilly.com? I got a lot of mileage out of that one also because he walks through all of the steps including making assets from scratch and animating their timelines.

  2. August 29th, 2009 at 13:53 | #2

    Very Helpful. Thank you.
    Will Goldstone does have a book that will be printed. It is currently available for download. I purchased it and it was very helpful.

    http://www.packtpub.com/unity-game-development-essentials/book

    I am still in need of scripting help. Thanks for your post.

  3. October 6th, 2009 at 12:46 | #3

    Hi there, the finished book has just been published..

    http://www.packtpub.com/unity-game-development-essentials/

    enjoy!

  4. pete
    January 2nd, 2010 at 02:55 | #4

    just wondering, Unity Indie is free? but when I am about to download it says 30 days trial. Sorry, I am super new on this

  5. Peter
    January 3rd, 2010 at 23:26 | #5

    Hello
    I am new to game development and also new to Unity and I found that there is a big gap related to Unity Education. If you come from game development, maybe not, but for me, learning Unity means copy mand paste of the examples any good fellow posted and when I try to analyze the scripts I discover that they used this or the other special way for having that effect that I dont understand but I end up copying it.

    I checked the book mentioned here (Goldstone) and he picks up some parts of some game and we follow it. So, my concern is that we (newbies) will be able to do that and a little bit more.

    So, I am wondering if there is something more organized in regards to Unity learning, because I grasp this knowledge of how to make a FPS, then this other of how to make the camera move, then this other of how to shoot; but I dont feel that I can do something myself nother than repeating (or copying) the scripting tricks someone did, so I dont fell I can CREATE something new.
    Please let me know if there is something that could take me from A, then B, then C.. up to Z and no A here, F there, H over there and then you figure out how to get B,C,D,E,G.. etc.

  6. slpetrov
    January 4th, 2010 at 09:37 | #6

    @Peter
    I am in the same boat as you. I have my education and background in web design and development, so my knowledge of game coding practices and design patterns are pretty much nil.

    What my approach is starting to be right now is getting other game development books using other languages like C++. Not everything is pertinent to using Unity, but a lot of books explain how to put a game together in terms of the architecture and game logic. Programming is pretty similar across languages, so it is a matter of converting API calls and syntax changing. I am reading a “Game Coding Complete” book right now. It is alright, but there might be one better for Unity people.

    http://www.amazon.com/Game-Coding-Complete-Third-McShaffry/dp/1584506806/ref=sr_1_1?ie=UTF8&s=books&qid=1262615435&sr=8-1

    It was the best book I could find at Borders, so I picked it up (I had a Christmas gift card as well and a coupon). There seems to be a lot of game coding books on the internet, so I am not sure what the best one is.

    @Pete
    Unity indie is Free. When you download it, you will get two different install options, one is the free indie, and the other a 30 day pro version. Hope that helps.

  7. May 27th, 2010 at 05:54 | #7

    I greatly profited from this example, to explain the notion of
    mappings to my Creative Technology students at the University of Twente, see: http://www.cs.vu.nl/~eliens/create/@s5-unity-walk.html
    (hint: draw on the movie canvas, or walk in space)

  8. July 13th, 2010 at 07:47 | #8

    nice script so ne script send me

  9. October 18th, 2010 at 03:16 | #9

    Hi… i’m getting Error!!! like inserting script
    “Script PlayerControllerMove has not finished compilation yet. Please wait until compilation of the script has finished and try again.”

    “Assets/Walk_Idle_Animation.js(5,26): BCE0019: ‘runSpeed’ is not a member of ‘Object’. “…

    what is the solution???

  10. Scott Petrovic
    October 18th, 2010 at 11:03 | #10

    I am not entirely sure, but based off the error, it looks like Unity is looking for the runSpeed variable and can’t find it in your Walk_Idle_Animation.js script. Where do you create that variable? Are you referencing it from another script?

  11. Don
    November 28th, 2010 at 13:53 | #11

    this was very helpful thank you

  12. March 28th, 2011 at 10:02 | #12

    can u please give me the code for moving the character like a real human i am new to unity 3d……….

  13. June 28th, 2011 at 04:49 | #13

    Iv just started to use unity, its my first venture into games creation so im not really sure if unity is for me really. Scripting is something im yet to try, something to look forward to by the sound of it!

  14. replay
    August 4th, 2011 at 18:20 | #14

    @pete
    Hi, yes Unity3D Indie Edition is totally free, 30 days trail is for Unity3D Pro, register and choose the indie version for free!(register in the link the installation gives you)

  1. May 2nd, 2010 at 07:02 | #1
  2. June 22nd, 2010 at 12:37 | #2