Category Archives: Assignments

Exponential Growth Simulation

I’ve been working at improving my Unity3D skills. We are using Unity in the Game Programming class I’m teaching this semester. I wanted to build a project that focused on creating some GameObjects that interact with each other through various public methods. So I made a growth simulation that represents a group of bacteria eating and reproducing.

The game objects include bacteria, food, and a game manager. All the food object does is keep track of how much food is available, and provide a public method called supplyFood(), with which the bacteria can acquire food to eat. The game manager object creates a grid of food tiles to be used as a food supply. It also keeps track of the number of bacteria in the game through public methods addBacteria() and removeBacteria().

The bacteria does a number of things. I tried to make this object class self-managing. So it has a timer that makes regular calls to the eat() method, and reproduce() method is based on doing a number of eats. There is also a die() method that takes the bacteria out of the game. But it doesn’t completely work correctly yet, I’m still debugging it.

One tricky part was finding out if the bacteria was touching a food tile, so it could eat. I used a collision detection method to initiate that. It is interesting how objects can connect with one another. Here is an example of how the bacteria connects to the food.

These variables are declared to give access to the foodTile object and the supplyFood() method of its food script:

  public GameObject foodTile; // the food object 
  public food food; // the food script found in the foodTile object; provides access to the supplyFood() method. 
  private int foodAmt; // the food counter

When a bacterium collides with (sits upon) a foodTile tagged “food” it opens a connection to the script it contains called “food.” I was concerned that doing this each time a new bacterium is created and collides with a foodTile, it would be a lot of processing overhead. But so far, it seems to be working.

   void OnCollisionEnter(Collision collision)
   {
      if (collision.collider.gameObject.tag == "food")
      {
         foodTile = collision.collider.gameObject; // connects the foodTile to this bacterium
         food = foodTile.GetComponent<food>(); // the food script of foodTile gives us supplyFood() method
      }
    }

Here is the eat() method that uses that uses the food script of the foodTile.

     public void eat()
     {
         int foodAmt = food.supplyFood();
     }

It took me a while to understand how getComponent works. It basically allows access to any component of a given object, including scripts. object.getComponent<component>();

Here is a video of what it looks like in Unity3D right now:

Character Following Camera in Unity3D

One thing beginning game developers using Unity 3D might want to do early on is to have a 3rd person character that is followed around by a camera. It is fairly simple to accomplish, but it can be tricky if not done correctly.

Here are the steps.

  1. Import Assets -> Import Package -> Characters. (Accept the defaults).
  2. Import Assets ->Import Package -> Cameras. (Accept the defaults).
  3. Make a ground object.
    • create a cube object (Game Object -> 3D Object -> Cube)
    • rename it ground
    • set its scale property to (100, 1, 100)
    • add a texture image (just import one and drag it to the ground object)
  4. Add a Multipurpose Camera Rig
    • In the Project tab, find the following: Assets -> Standard Assets -> Cameras -> Prefabs -> MultiPurposeCameraRig
    • Drag and drop the MultiPurposeCameraRig prefab to the scene window
    • Disable the MainCamera by selecting it in the Hierarchy window, then deselecting the checkbox next to the “Main Camera” name in the Inspector window.
  5. Add a 3rd Person Character
    • Also in the Project tab, find: Assets ->Standard Assets -> Characters -> ThirdPersonCharacter -> Prefabs -> ThirdPersonController
    • Drag and drop a ThirdPersonController prefab to the scene window
  6. Connect the Multipurpose Camera Rig to the ThirdPersonController
    • Select the MultipurposeCameraRig object in the Hierarchy window.
    • Drag the ThirdPersonController from the Hierarchy window to the “Target” field of the MultipurposeCameraRig’s Auto Cam Script found in the Inspector panel. (Should be dragging across from the leftmost panel to the rightmost panel.) Connect Camera to Character
  7. That’s it. Play the game and if everything is setup correctly, you should have a camera that follows the player.

Instantiate a Prefab using Code in Unity 3D – Part 2

In our previous post, we learned how to use code to create an instance of a prefab object in Unity3D. In this post, we will see the power of using code to do repetitious work. Whenever I think of using code to do something that repeats, I think of using loops.

Last time we created a graphic that represented one single step of a staircase. Now we will create many instances of that same graphic, and move them in such a way that it builds a staircase. To do that, we will use the famous “for/next loop”

instPlank4Here is our single step. Now let’s make some more to create a staircase.

Open the “MakeStep” script from last time. Look in the Start() method for this line of code:

Instantiate(stepPrefab, stepPos, Quaternion.identity);

This is the piece that brings our stepPrefab into our Unity world. Now, let’s add a for loop around it to get it to repeat and build a “Stairway to Heaven.” Your code should look something like this:

for(float i = 0; i < 20; i++) 
  { stepPos.y = i;
    stepPos.z = i;
    Instantiate(stepPrefab, stepPos, Quaternion.identity);
  }

If we run the code at this point, we’ll get a staircase that looks like this:

instPlank5

The y values are too far apart, and the z values will overlap. I want my staircase to be useable by a character in the game. So I will modify my loop to bring my steps closer together.

for(float i = 0; i < 20; i++) 
  { stepPos.y = i / 4;
    stepPos.z = i * 2;
    Instantiate(stepPrefab, stepPos, Quaternion.identity);
  }

This is more like it:
instPlank6Now, we just have to add a floor, and a character to the scene, and we’ll be in business!

To make a floor, create a new cube. Set its position to (0,0,0). Give it a texture using a graphic or color using a material. Set its scale to 100 x 1 x 100.

To add a character to the scene, Import the characters package, accepting all defaults. It will take a minute or two to bring in all of the stuff for characters. It is found under Assets -> Import Package -> Characters.

instPlank7

After everything installs, in the newly created “Standard Assets” folder, go to Standard Assets -> Characters -> FirstPersonCharacter -> Prefabs and select  the FPSController prefab. Drag it into the scene. Because the character has its own camera, you should disable the Main Camera in the hierarchy.

You should be able to play the game and move around in the world you just created using the mouse and arrow keys. Here is a view from beside the staircase.

instPlank8So that is my introduction to creating prefab objects in Unity3D using C# code.

Students and Educators can get Unity3D for free so you should try it. Download it here.

 

Instantiate a Prefab using Code in Unity 3D – Part 1

This year, in the Game Programming course I teach, at the urging of my students we have begun working with Unity3D. I’ve always used Flash in the past for this course, but some things have changed for me both personally and professionally to where I felt I could spare the time to begin with a new development platform. Any interested students or educators can learn along with us by downloading Unity3D for free.

For this post, we are going to discuss creating prefab objects (just a saved game object that has some preconfigured properties added to it) and using code to create instances of those prefabs.

To begin, let’s make a prefab object by creating a new cube (Game Object -> 3D Object -> Cube) and setting its position at (0,0,0). Rename this cube to “stepPrefab.” Scale the x dimension to 10, and the z dimension to 2, and the y dimension 0.25. This will create a plank-like block that will become a step in what will later become a staircase.

instPlank

Decorate the step as you like using materials or graphics. Online, I found an image of bricks I used to make a brick texture, but you can also simply create a material and set its color to use. Once you have decorated the step, create a “prefabs” folder in the project panel under Assets. Drag and drop your decorated brick into the prefabs folder. There it will become a prefabricated object or “prefab” that we can manipulate with code. Delete the stepPrefab object from the hierarchy tab, but leave it where you put it in the prefabs folder. Except for the light and camera, our world should be empty again.

instPlank2

Next, create a new folder called “scripts” under Assets, also in the Project panel. Create a new C# script inside of the scripts folder, either by right-clicking or clicking on the Create button. Name this script to “MakeStep.” Double-Click the “MakeStep” script to open it in an editor.

Just inside of the MakeStep class, declare a public variable of type Game Object called “stepPrefab” and a second of type Vector3 called “stepPos” at the game’s origin position. That code looks like this:

public GameObject stepPrefab;

public Vector3 stepPos = new Vector3(0f, 0f, 0f);

Now, inside of the Start() function write a line of code that will instantiate (create)  an instance of our stepPrefab object. Here’s the code:

Instantiate(stepPrefab, stepPos, Quaternion.identity);

Here, the Instantiate function creates a new stepPrefab object, places it at the stepPos position, and uses no rotation (that’s the Quaternion.identity bit.)

Save and build the script. Now, switch back to Unity3D. Create an empty game object. Give it a name “stairs.” With “stairs” still selected, browse to the folder containing the MakeStep script, and drag that script on to the “add component” area of the inspector window. This should add the MakeStep script to the empty “stairs” object, now visible as a component of “stairs” in the inspector.

Inside the script component just added, locate the field box called “Step Prefab.” Right now, it should say “None (Game Object).” This is where we will make the connection between the Unity 3D world, and the MakeStep script. To do this, drag and drop the stepPrefab object in the Assets/prefabs folder over to the Step Prefab box in the inspector window. Now our world and our script should be connected together.

instPlank3

If everything is in order, it is time to test our world. Click the play button in unity. You should now see an instance of the stepPrefab object.

instPlank4

Next up, we will build a set of stairs using this object.

Unity Programming – A New Platform.

I’ve been teaching the Game Programming class with Unity 3D for two weeks now. This is the first time we’ve used Unity in the class. It’s been interesting, and a challenge to my previous knowledge on the subject. In previous iterations of the course, we used Flash and Actionscript.

Something new I learned about recently is the comparison between inheritance and composition in object oriented programming. I hadn’t really encountered composition before, only the inheritance approach. So when diving into Unity, I didn’t really see OOP with inheritance and I wondered why that was the case. It turns out the Unity uses a composition approach.

Basically, instead of having a superclass and subclasses that all must conform to or override the superclass, each object is assembled out of components or capabilities.

This old JavaWorld article explained it nicely for me. https://www.javaworld.com/article/2076814/core-java/inheritance-versus-composition–which-one-should-you-choose-.html

Denny’s Blog

I am always learning new things from my students. This week, I learned about the Denny’s Restaurant blog. It is full of wonderful, punny food GIFs like this one:

Hamboni Pun
Denny’s Hamboni

This is a great example of self-deprecating humor that shows it is ok not to take one’s self too seriously. Certainly this approach cannot work for all businesses, but it seems to be working for Denny’s.

I like the Denny’s work so much, I challenged my students to make a series of food GIFs and release them in their own social media campaign on Twitter. We’ll see what happens.

Fire Writing

Lia Fire Writing
Lia Fire Writing by naturalturn 2008 https://www.flickr.com/photos/naturalturn/
I don’t think I’ve ever heard the expression “fire writing” before, as mentioned in this Edutopia article, New Teachers: Inspire Your Students to Write, Write, Write. However, it is exactly how I got things going with my PhD dissertation. I just wrote. And I didn’t worry about how it looked or sounded. It was messy and chaotic, but I cleaned it up later. First I dumped my ideas out of my brain and onto the page as quickly as I could. Then I went back and ruthlessly edited. I cut out huge chunks that I loved and was left with the essence of what I was trying to say.
 
Another great idea from the Edutopia article is something I first read about years ago in Zinsser’s classic book, Writing To Learn. Students tend to write for an audience of one – the teacher. They need to stop it, and we as teachers should stop encouraging it. Zinsser suggested writing letters to a friend or family member about the day’s learning.  I think it is a great approach. Instead of getting hung up on perfect mechanics, get to the main ideas, reflect and write about them. Quit worrying about what you think the teacher might want, and write the important ideas in a voice that someone who loves you and cares about you would recognize as you. If it comes out wrong, you can always improve and revise.
I teach a class that includes a lab activity and requires lab reports. I wonder what those might look like if they were composed in the form of a letter? Right now, I have a prescribed format that I’ve been using since I first started teaching the course. I inherited the report structure from the person who taught the class before, and haven’t ever questioned it. Perhaps it is time for me to take a closer look at that important writing assignment.

An Audience of One


Photo Credit-Jim Richardson

I just visited a website of an unnamed person doing the DS106 digital storytelling course and I experienced a classic error that my own students tend to do when I ask them to publish school work in a blog format. That person wrote for an audience of one – the teacher. There was a generic reference to the chapter, but no indication of what the text might have been. So a random visitor from the Internet – me, has no context for what I am reading and no way to learn more about it myself.

Have you ever stumbled into a conversation that really wasn’t meant for you? Awkward, isn’t it? That is what these sort of posts do to the reader. How much better would it be to envision a broader possible audience and write to that audience? After all, whenever you publish anything to the web, indeed you do have a very broad potential audience, don’t you?

I think students everywhere would do well to adopt this practice in all of their classes, and their writing would immediately improve. Stop writing for an audience of one – the teacher, and start writing for a broader audience. What might your grandmother want to know about this topic? A long-lost friend? A random stranger on the street? How would having a larger audience upon whom you would like to make a good impression affect the tone and style of what you write?

If you will simply write for this larger audience in mind, and stop writing what you think your teacher might wish to hear, everyone involved will appreciate it, including your teacher, because you will be doing better writing.

 

 

Happy 50th Star Trek!

Fifty years ago, one of the best television shows ever made was first aired – Star Trek! We love Star Trek at my house. A few years ago I made this video of myself and I thought it appropriate for today.

But then I saw this tweet:

So we made this video in our Digital Media class. Sulu, get those shields up!

Thanks to Paul Bond for sharing the idea with us and the commentary. http://blog.raptnrent.me/2016/09/08/awesome-moments-of-spontaneous-creativity/

PS I made a vector of Spock in celebration of Star Trek 50 too!

Spock Vector Drawing

What A Crappy Font Will Do

John Deere Logo

This is a DS106 visual assignment that challenges us to remix an existing corporate logo with a bad font (or an improved font). At 4 stars, I think the difficulty rating is somewhat inflated. Granted, I’ve been doing this kind of work for a while, but this assignment only took me 5 minutes to complete. It is taking me longer to create and publish a post about it.

My process used was simple. I did a Google image search for the “John Deere Logo.” Then I downloaded the image and placed it into Adobe Illustrator. I used a clipping mask to remove the old John Deere Text. Then I used the text tool to reset the type in the over-used Papyrus font. I then saved the image as a PNG graphic and uploaded to this website. Boom- done! My rating for the assignment difficulty was a 1 star. For a beginner with no graphic-making experience, it might take a little longer, but certainly no more than 2 or 2.5 stars.

So to take it up a notch, I followed Paul Bond’s lead and put the Papyrus font onto a John Deer product.

This took more time than the original assignment. I had to find a tractor using Google once again. Then I used the clone tool of Photoshop to remove the original John Deer lettering. I tried using Photoshop to add the lettering back in, but I wasn’t pleased with how it turned out, so I saved the edited photo as a JPG and brought it back into Illustrator, where I used the text tool to set the type. I then saved the photo as a web ready JPG image. Here is the result:

John Deere Tractor