Unity -The Room/Walking Sim

While learning Unity we’ve been given an ongoing project. To create an environment for a walking simulator in the same vain as titles such as Gone Home, Dear Esther etc. Week on week, as we learn more about the software, we can add new features. While feature creeping is generally regarded as a bad thing, it works in this content for slowly learning Unity and C#.

Using the create tools, we were tasked to make a simple box room. Done!

emptyroom

 

A first person camera controller was added, much like last week, and I changed the colour of the default directional light along with adding some basic material colours to the walls/floor so it wasn’t so bland. To assist in asset management I created an empty game object, called it Room1 and then assigned walls/floors to it. This helps for 2 reasons:

  1. It keeps things organised in the hierarchy and allows you to collapse all the entries that make up the room down to one. This will be helpful in larger projects.
  2. Treats the room as one object which can be moved around without having to move individual parts.

Time for a trip to IKEA! No, not really, but furniture did come next. The class was given a few models to throw in and I quickly jumped into Maya to throw up a bookshelf. Here’s the result:

walksimroom

The scene was set to start learning some basic code. I’ve said this before, but this is the one part of the whole course that just doesn’t come naturally to me. Code is some form of wizard sorcery I need to decipher, so I better find my wizard hat if I’m going to get through this.

The Key

Okay, first of all the task was to give some interaction to the key. Simple logic: collect key, remove key from game (since it’s been picked up) and have the door open to leave the room (then fall eternally as nothing else exists). So I’ll break this down for my own benefit, too:

public class KeyController : MonoBehaviour {

public GameObject door;

void OnMouseDown() {
Destroy(gameObject);
Destroy (door);
}

  • Public GameObject door – Creates a public game object variable named door; this will show up in our components inspector and we can assign an object to it.doorcomponent
  • Keycontroller is the name of the component script that this code is contained in.
  • voidOnMouseDown – Starts describing what we wish to do; in this case, when the mouse button is clicked on the object this script is attached to, do what follows.
  • Destory (gameObject) – Destroys the game object this script is attached to.
  • Destory (door) – Destroys the object ‘door’ in the scene, which we made available earlier in the script with the line ‘public GameObject door’.

So, after associating the game door with the door in the component, when we click on the key, it should vanish and when we turn around… voila, the door has vanished, too.

Adding Text to the UI

public class Highlighter : MonoBehaviour {

public string myName;

void OnMouseOver() {
GameObject.FindObjectOfType<Text>().text = myName;
}

void OnMouseExit() {
GameObject.FindObjectOfType<Text>().text = “”;
}

}

  • Highlighter – Same as above, this is the name of our component script where code is held
  • Public string myName – Creates a public string of text with the name of ‘myName’; this will show up in our components inspector and we can write in it.myname_takethekey
  • OnMouseOver – Similar to the above, however this activate whatever code we nest into it when the mouse pointer is hovered over the object and not clicked.
  • GameObject.FindObjectOfType<Text>().text = myName; – This looks for a text object in the scene (which we have created, sitting in the hierarchy) and displays whatever text is held within the myName variable. In this instance it displays ‘Take the Key’.
  • OnMouseExit – When mouse is no longer over the object, run whatever code we nest into it.

So in English we hover our mouse over the key and text will pop up and say ‘Take the Key’, when we remove our mouse pointer from the object the text will vanish. Although, technically we’re telling the UI to display an empty text box not vanish, however it makes no visual difference to the player.

Wrap Up

At the end of the session we were all encouraged to submit an idea for features we’d like to see implemented and learn about next week. My bank of desks suggested a small inventory system, so objects like the key could be carried rather than destroyed. This was taken on board and I look forward to this next week.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s