Rock, Paper, Shotgunity, Post 12

By James Carey on January 20th, 2010 at 1:32 pm.


I’ve realised that the relative complexity of the Shotgunity project might be distracting from a central idea I’ve wanted to get across in this series, namely that it’s easy to make games with the Unity tools. To prove this point, today, together, we’re going to make a clone of a giant in gaming ancestry, we’re going to make Pong in a post. And not even an overly long post at that. Grab a copy of Unity and meet me after the cut.


Once you’ve got Unity downloaded and installed create a new project. Name it PongInAPost. The default Unity interface is made of 5 windows or tabs: The Scene view, the Game view, the Hierarchy tab, the Project tab and the Inspector tab. They’ll all be pretty empty at this point. In the main toolbar at the top of the screen go GameObject>Create Other>Sphere to create the ball for our pong game. Note the Inspector tab is now full of details for this object. In the Inspector tab make sure the X, Y and Z position values are all set to 0. The object itself, ‘Sphere’ is now listed in the Hierarchy tab. Above the Sphere object in this Hierarchy tab you’ll see another object ‘Main Camera’. Highlight (click on) this object in the Hierarchy tab and from the main toolbar again go GameObject>Move To View. In the Scene view you’ll now see that the Sphere has a new icon in the middle that looks a bit like a camera with three axis arrows attached to it. Grab the Blue arrow with the mouse and push it back until you can see the Sphere appear in the Game window below When you move the camera with this axis arrow note that the Z position value changes in the Inspector tab. You can directly adjust this value in the Inspector tab to be more accurate. Change the Z value to about -10 and hit return. The sphere should now look pretty small in the Game window.


Now go the main toolbar and go GameObject>Create Other>Cube. This will create a new cube object in the same place as our Cube (unless you’ve deviated from instruction and moved the Scene view… Naughty!). Grab the green axis of this new cube and move it up a little. See the Y position value of the cube changes in the Inspector tab to match. In the Inspector tab, change this value to about 6.

There’s a third way to adjust the transform values of object and that’s using the icons at the top left of the Unity GUI. Cick the last one in this row of four (Scale the Highlighted Object) and – making sure the Cube object is still highlighted blue in the Hierarchy tab – hover the mouse anywhere over the Scene view and hit the ‘f’ key. This will focus the Scene view on the Cube object. Now grab the Red axis on the cube and pull it until the Cube has stretched out into a long bar (the X scale value in the Inspector tab should end up being about 12. Adjust the value directly in the Inspector tab if you prefer).


With the Cube object in the Hierarchy tab still highlighted hit crtl+d to duplicate the Cube and change the Y position value of the new duplicated cube to about -6. You should now have two long bars above and below the Sphere in the Game view. Duplicate a third cube (select one of the bars you have in the Hierarchy tab and hit ctrl+d) and move this third cube along it’s Y position axis back near to the Sphere. Change it’s X and Y Scale values in the Inspector tab to 1 and 2 respectively. You should now have a stumpy pong paddle near the Sphere. Adjust its X position value until it’s at one side of playing area but still inside the top and bottom bars, i.e don;t move it beyond the end of the long bars (see screenshot).

Duplicate the paddle and move the new one over to the other side of the play area. We now have four objects called simply ‘Cube’ in the Hierarchy tab and this might get confusing so we can rename them by right clicking on the object in the Hierarchy tab and then Rename, just as you would in windows explorer. As with explorer you can also do two slow clicks on the object to rename it. Try this as we’ll need to use this method in a minute. You can zoom the Scene view out with the mouse wheel a so you can see what objects you’re highlighting. It’s probably a good idea to save. Go File>Save Scene from the toolbar and call it whatever you like.


Now we’ll make the paddle controls. Right click on a blank part of the Project tab and go Create>JavaScript. This will create a new script component called NewBehaviourScript in the Project tab. Rename this “Player1controls” using the slow click renaming method. In the Inspector tab you’ll see an Edit button for this new script. Click it to open the script editor. Paste the following code into the new document, replacing any text that’s there:

function Update () {
if (Input.GetKey (“q”)) {
transform.Translate (0, 4 * Time.deltaTime,0);
}
if (Input.GetKey (“z”)) {
transform.Translate (0, -4 * Time.deltaTime,0);
}
}

This code just checks to see if the Q and Z keys are pressed then moves whatever the script is attached to up or down accordingly. Save this script and return to the main Unity screen (i.e minimize the script editor…). Make another new script in the Project tab and name it “Player2Controls”. Add the SAME code to the new script but change the letters q and z to o and m or whatever letters you’d like to use for player 2 controls.


We’re nearly there! Drag the Player1controls script from the Project tab onto one of the paddles, then drag Player2Controls onto the other. If you click the Play icon at the top center of the screen now you’ll preview the game and see that the keys control the paddles.

Now let’s give the ball some movement. Highlight the Sphere object in the Hierarchy tab and go Component>Physics>Rigidbody from the main toolbar. You’ll see in the Inspector tab that this has added a Rigidbody component to the list of components. Uncheck the Use Gravity box you see there. Change the Mass value to 0.01. A little above the Rigidbody component in the Inspector tab you’ll see a Sphere Collider component. Change the Material value to Bouncy by clicking the tiny arrow at the end of the value and selecting it from the drop down menu.


You should now see a small icon and the word Bouncy in the value part of the Material of the Sphere Collider component. Click this icon and you’ll see a yellow box flash up in the Project tab to highlight this Bouncy material. Drag this material from the Project tab onto both of you paddles and the top and bottom bars to add bounciness to all your gameobjects (apart from the camera…).

We’ve got two more things to do. Add initial force to the ball and respawn it if goes beyond the edge of the play area. First we’ll add force. Create a new Javascript in the Project tab and call it BallMover. Click edit in the Inspector tab and replace the text of this new script with:

function Start () {
rigidbody.freezeRotation = true;
yield WaitForSeconds (2);
rigidbody.AddForce (Random.Range(3, 5) , Random.Range(3, 5) ,0);
}

function Update () {
transform.position.z = 0;
}

Save it and drag this BallMover script from the Project tab onto the Sphere object in the Hierarchy tab. If you click Play now to preview the game you’ll see the Sphere move in a random direction with random force (between a range). This is almost the game done, but we want to respawn the ball if it goes beyond the play area. So finally, from the main toolbar, go GameObject>Create Empty. Rename this new gameobject “OutOfBounds” in the Hierarchy tab. With the object highlighted go Component>Physics>Box Collider and in the Inspector tab tick the isTrigger box. Place this OutOfBounds object at the edge of the play area, and change the Y scale value in the Inspector tab to about 12 or whatever covers the whole side. Now create a new JavaScript in the Project tab and name it “RespawnBall”. Add the following code to the RespawnBall script replacing whatever’s there:

function OnTriggerEnter (other : Collider) {
other.transform.position = Vector3(0, 0, 0);
}

Lastly, drag this RespawnBall script from the Project tab onto the OutOfBounds object in the Hierarchy tab then duplcate (ctrl+d) the OutOfBounds object and place the copy at the other end of the play area. Save and preview the game.


If it’s all working as expected, go File>Build Settings… then click the Add Current button in the build window that pops up. Make sure WebPlayer is ticked and click Build. Call the build anything you like. Unity will then export and build the project and you’ll end up with a webpage you can open in your browser and play your pong game.

There you go, Pong in a post. You can grab the Project Files for this super quick pong from here to check your version matches mine. Id like to see your versions and what improvements you can all make, so why not post yours in the Forum?

Now, I’d better stop procrastinating and get on with Shotgunity…

__________________

« | »

, , .

50 Comments »

  1. Poet says:

    My obligatory post asking if any of the newly free engines are capable of modeling a fully working spaceship the size of say Serenity.

    • Bhazor says:

      Yes. Every game ever made can make a working model of a spaceship.
      Now be quiet and get some decent taste in Sci-fi. Farscape, Stalker or the original Babylon 5 for example.

    • Psychopomp says:

      >Firefly
      >Bad taste

      laughingelfman.jpeg

    • sana says:

      >greentext
      >implying

    • Jakkar says:

      Depends what you mean by ‘fully working’ – do you mean a physics simulation whereby the engines actually work based upon their fictional qualities, or just a 3d model with lights and doors that open?

      <3Firefly ;D

    • Bhazor says:

      Yes Firefly was good. All 14 episodes of it.

      But the way a certain sub set of nerds bang on about it you’d think it was a lost series of Flying Circus or an unreleased Beatles album. It’s an above average sci-fi show thats only remarkable feature was that it made it to tv which was pretty rare for Sci-fi pre Doctor Who Two. In comparison to the whole cannon there’s really nothing remarkable in its setting (bilingual world was done in Blade Runner for example) and characters (River is just Oasis). Farscape on the other hand had organic spaceships, a setting where humans were the villains, aliens supplied by Jim Henson and Claudia Black in leather trousers. Thats the kind of innovation I can get behind.

      @Jakkar
      Once again, every single game engine ever made could simulate a spaceship of any size. You could probably do it in Basic, Ascii or Scumm. Besides a fully modeled Moya or Babylon 5 station would be much more impressive.

    • DarkNoghri says:

      In my mind, Firefly was never about having an amazing setting. It was about the characters and their interactions. The best part of that show was the writing and the stories, which I have yet to see topped by anything else.

      Which is why Firefly (all 14 episodes of it) is still my favorite TV show of all time.

    • Poet says:

      I’m so sorry I didn’t pick one of your favorites Bhazor, had I would I still have to be quiet? BTW, what game are you referring to and what engine did it use? Mind I want to be able to get in and perform a crazy ivan and be able to fly out into space with it, I mean what kind of space ship would it be without those simple functions? I quietly await your reply.

    • Bhazor says:

      Well given a crazy Ivan is just a bootlegger’s turn I’d again say any driving game made after the mode 7 era could recreate it.
      As for modelling a (dull) spaceship. Just open your design suite, open design object tool, create an object,define object as ship, you have created a model of a ship. Everything after is just programming and art which could be done in anything. Heck, crazy Ivan was the only move in Asteroid.

      But seriously, you were becoming a bit of a troll with the same post in every thread about Shotgunity.

    • Poet says:

      So in reality you can’t answer my question and are just annoyed I keep asking it. I propose YOU keep quiet then and I will keep posting my question until I get an answer that has more substance to it then “any driving game made after the mode 7 era could recreate it” which of course isn’t even remotely close to answering the actual question.

      If you want to reply I suggest you re-read my question.

  2. Brulleks says:

    I wondered what the hell ‘Spong in a posty’ was all about before reading the rest of the article.

    Now, Serenity would be a class use of this toolset. Aiming to resurrect the Firefly MMORPG, Poet?

    • Richeh says:

      Hah! I was going to write a sarcastic post about that, too, but I decided nobody would know what the ballflaps I was talking about. Looks like I was wrong.

    • medwards says:

      I’ve been reading too much of the Solium Infernum story so I thought it was about Sponge… Sponge’s… lady bits… because I thought it said ‘Spongina.’

    • Poet says:

      I guess if you had 50-80 million, a couple hundred depressed overworked code/art/design monkeys and a few years time.

      No, I’d simply be happy moseying around the cargo bay with the cows.

  3. Doove says:

    SPONG IN A POSTY

  4. AndrewC says:

    James have you considered changing Shotgunity’s name to SPONG IN A POSTY? The first rule of Indie game development is having an attention grabbing name. I don’t think you could do better.

  5. hydra9 says:

    I have to admit I sat there for a good 20 seconds being confused and reading and re-reading ‘Spong… Spong?’

    Anyhow, I’ll try this tutorial out when I have some time later.

  6. Frye says:

    You can make anything on unity. Unity has already proven itself to be robust and powerful. The latest EA Tiger Woods PGA tour golf online was made on Unity. I am playing the beta right now and i am well impressed (about the technology, NOT the 8 quid/ month they want for it). No disk required, yet no delays at all.

    There’s a few demos on the unity site, but they don’t really come close to what EA did with it.

    The beta is now closed, not sure why i am still allowed in (buying loads of AE games might have helped), next beta will be open and start ‘early 2010′.

  7. Altemore says:

    I got as far as adding the scripts to the paddles. The meany Unity tells me the scripts haven’t compiled yet :( What do?

    • James Carey says:

      Did you create the scripts in the way I described or did you try to make a new script from within the script editor (file>new etc)? If the latter then it can take a while to refresh and compile in my experience. You’re better off creating a new script by right clicking in the Project tab as described in the tutorial then renaming it, then editing. If you DID do it this way round then me shrugs… You could try saving everything then restarting Unity, at which point it’ll compile all scripts.

    • Altemore says:

      I restarted and it did indeed compile. But alas, my troubles are not over, it tells me I can’t add scripts before fixing all compile errors -_-

    • LeFishy says:

      I am assuming you are using the Windows version of Unity.

      I don’t because I found it a painful, slow experience compared with the OSX build.

      It also tends to bluescreen my computer…

      But yeah just to say you’re not alone if you’re using the Windows version.

    • James Carey says:

      Double check you’ve copied the script correctly from the post. You can grab the scripts directly from Project files from the link alternatively. It all compiles and works here!

      Ah… I did just notice a typo in the post for the BallMover script which I’ve now corrected, but you hadn’t got that far so it couldn’t be that anyway…

    • Altemore says:

      I checked and everything seemed to have copied over fine. Your files work wonderfully, making me all the more ashamed that I can’t create a friggen pong clone :i

    • NukeLord says:

      @Altemore: Try deleting the “q”, “z”, etc from the code and retyping it, that worked for me. No idea why though.

    • Colthor says:

      @NukeLord:
      Probably because the “smart quotes” copy as two different unicode characters, rather than the normal quotation mark character that the compiler’s expecting.

  8. BooleanBob says:

    Something about the wording of the opening paragraph made me think we were going to make a clone of a Giant Crab in Unity, which frankly made the rest of it a little disappointing.

  9. MacBeth says:

    I invented a game called Spong in my youth. It was played with badminton rackets and a soft sponge tennis ball, on a ‘court’ drawn across the width of the road. If only I’d trademarked the name I could have got all Tim Langdell on your asses and sued for licensing rights…

  10. Joseppe says:

    @Bhazor
    Having just watched Farscape in its entirety over the past couple of months, I can safely say that Firefly > Farscape.

  11. Nighthood says:

    >Implying Chan references are in anyway required or desired in an RPS comments thread.

    • Nighthood says:

      That reply button will be the death of me.

    • Bhazor says:

      Could be a worse fate than Chan references and running jokes.

      - KG

    • Chris D says:

      @Poet

      Final Fantasy 8 had a spaceship you could pilot and walk around in. Might have been one in 9 as well, although if you count airship as close enough then you can pretty much the entire series.

    • Chris D says:

      @me

      Replyfail, should have been down thread

  12. lol says:

    This is why we can’t have nice things…..

  13. Brumisator says:

    I absolutely vote for Rock Paper Shotgunnity to be renamed Spong ina Posty!

    Also, making pong this was has been fun , thank you, James Carey!

    Although I didn not have any materials when I tried to make the ball bouncy, but no matter, the easy to use Unity help got me rolling in seconds.

    Now, to add phong shading, terrain deformation and TCP/IP gameplay…see you all in a few years.

    • James Carey says:

      Yer the Bouncy material is dependant on you having the Standard Assets package imported in the new project. As far as I’m aware the Standard Assets are imported by default when you create a new project so unless you unticked something when you created your new project you should have them…

    • JellyfishGreen says:

      Yes, thanks for this!
      1st easy feature:
      Spong in a Posty + Unity_3D_Tutorial particle emitter = flaming pong ball with smoke trail.
      1st unsolvable bug:
      The paddles go through the walls. I need to give them a hard Y limit, rigidbodies won’t do everything…

  14. Calabi says:

    You dont want an answer to your question you just want to argue.

    In answer to your question no you cant do anything in Unity.

    • Poet says:

      If that’s the case then why do I keep spending my valuable time asking it? BTW, maybe I wouldn’t need to argue if people like you would stop posting such ludicrous answers like “In answer to your question no you cant do anything in Unity.” or “Once again, every single game engine ever made could simulate a spaceship of any size.” .

      Since you both seem to not be able to find my original post let me quote it again here with emphasis on the keywords.

      “My obligatory post asking if any of the NEWLY FREE engines are capable of modeling a FULLY WORKING spaceship the size of say Serenity.”

      And by fully working I mean get in and fly around, both in space and atmo.

      By all means go ahead and answer with more uninformed comments and I will be sure to argue back.

    • Hattered says:

      @Poet:

      This should meet your needs. Though you may find better suggestions on a dedicated forum. (Really though, your question is tantamount to asking if there are any free word processors capable of writing fanfic, which is why it is not being taken seriously. It’s less a question of the engine and more a question of the programmer. With dedication, you should be able to do it eventually with whatever engine you choose. If you want help choosing an engine that is easy to program for, it would be best to try on a forum dedicated to such things. Best of luck.)

    • Poet says:

      Heh, at first I thought you were being facetious with the Inform link but as read on it became clear its a first stepping stone type of thing and I thank you for it. Still not sure how they got Physics into the latest revision of their Interactive Fiction Engine though.

      Btw, the whole point about being a neophyte on a particular subject IS that you have no clue at all your questions are tantamount to a baby babbling to Einstein about physics, and again thank you for explaining that.

      Now, for the life of me I cant think of a single game that has ever let you both pilot AND wander around your spaceship. Anyone?

    • Chris D says:

      @Poet

      Final fantasy 8 had a spaceship you could fly and walk around in, although if you count airship as being close enough then you can have anything in the series

  15. wererogue says:

    Just to speed up your day, like explorer, rename can also be done with F2.

  16. ps3go says:

    - Brand New P3Go GT-Break G-Switch PS3GO GT-Break Modchip for PS3p3go
    - Have all function features of the first generation PS3 Jailbreak dongle.[url=http://www.ndscardsale.com/product/p3go-gt-break-with-g-switch-ps3go-gt-break-jailbreak-chipset-for-ps3-fat-and-slim-91812]p3go[/url]
    - Brand new the second generation configuration, support upgrade without Keys, plug and upgrade.P3Go GT Break

  17. ps3go says:

    Thanks for posting this.

  18. shanmugam87 says:

    I love your blog.. very nice colors & theme. Did you create this website yourself? Plz reply back as I’m looking to create my own blog and would like to know wheere u got this from. thankslive cricket streaming