Room Game in Unity (part 1)

Today in class, we covered the basics of starting our own Walking Simulator games in Unity. We had six goals for the end of class:

  1. Create an interactive room
  2. Use Unity’s hierarchy
  3. Add custom models to a project
  4. Develop our own components
  5. Experiment with the editor
  6. Suggest next game feature

Ant suggested that we made our floor 10 x 10 to allow ample space for movement in our room, since we’d be adding furniture and other custom models. I don’t know if I was supposed to, but I made a plane first, then put the cube that would become the floor intersecting that. I then coloured the walls, floors, and popped in an FPS camera.

basic-room

It all looks very basic at the moment, but Ant then instructed us on where he stored some assets we would/could use to furnish our room and eventually write some code for.

I ended up quickly modelling a bookshelf to add to the room, too, while others were finishing up their rooms and we were waiting for further instructions.

furnished-room

The hierarchy is something so important to remember and a clean, clear hierarchy (and project folders) makes everything easier to do. Without keeping things organised, you would forget what shape was what – this will become even more important when we have multiple rooms in our walking simulator game.

room-scene-1-hierarchy

 

Here’s a quick shot of my hierarchy by the end of the exercise. I grouped together my room’s floors and walls into an Empty Game Object, which I simply named “Room”. This meant that I could, in theory, move the room as a whole however I want, but also duplicate it should I wish to use the same layout as another room later on.

I could see this being important for bigger projects, but also for objects that make up a whole (perhaps a lap with its lighting or a bookshelf and books that can be moved, thus are separate objects).

 

Adding the objects was fairly straight-forward: add the objects (in this case, the .fbx files Ant provided us or the bookshelf I made in Maya) to the Assets > Models folder. They are then accessible in Unity as objects to be placed wherever in your scene. I did have a bit of trouble with scaling my bookshelf, because when I imported it the size was way too small and had to be scaled up tremendously. I will have to look a little more into the sizes between models made in Maya versus what they’ll translate to in Unity.

The next part we did was the most fun, in my opinion, which was writing new script for our rooms, so that we could interact with different objects and have different things happen. In this instance, we wanted to be able to pick up the key when we hovered out mouse over it.

using UnityEngine;
using System.Collections;

public class KeyController : MonoBehaviour {

void OnMouseOver () {
Destroy(gameObject);
}
}

This worked like a charm, although personally I think that the¬†deactivate code that we learned in the roll a ball tutorial [ gameObject.SetActive (false); ] might be more useful later on down the line. I could use it when, for example, the player enters a room and the door had been deactivated on entry, but reactivates to lock them in until they figure out the way out. I don’t think that can be done if you destroy the door completely from the game.

We continued our journey to get out of the rooms by actually changing our script slightly. Instead of just destroying the key and the door remaining in place, we also destroyed the door when the key is picked up.

using UnityEngine;
using System.Collections;

public class KeyController : MonoBehaviour {

public GameObject door;

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

The above code now adds a new part to our Key Controller script component when we associate it with the key: a door as a GameObject. We can now associate the door to the right key!

We’ve also written that, when we CLICK on the key (so OnMouseDown, not just putting our mouse over it), the key will be destroyed but so will the door behind us, thus letting us out of the room.

key-controller-component

All we had to do then was associate the door (click and drag the Door from the hierarchy and drop it into the space available) with the script. The GameObject “door” in the script now knows that we mean the “door” model in our game.

We also added another useful script, which we named Highlighter, that would bring up the name of an item as we hovered our mouse over it.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class Highlighter : MonoBehaviour {

public string myName;

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

First, and most importantly, we had to remember to tell the script that we were going to use the Unity UI, otherwise we would get a whole bunch of errors. After that, we added a new box to our component where we could write in a string (ie, the object’s name or some sort of telling text). When we hovered our mouse over the object, the string would appear on the screen. We asked Ant to check if there was a way to make the text disappear, because otherwise the text was just staying on the screen. He told us we could add the following code to only have the text string appear when we hovered over the object it was linked to.

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

highlighter-componenthighlighter-example

So, as you can see, by defining myName in the component as “Table”, when I hover over the table in the game it displays “Table”. I did the same sort of thing for my Key and the Door.

Getting the text to display was something we learned in the roll a ball tutorial, but it was useful to go over it again in this sort of situation; while in the hierarchy, go to Create > UI > Text. We could then position the text where we wanted it to appear on the screen (I chose the top left and anchored), the colour of the text (I made it white so I could see it better), size, font style, etc.

Towards the end of the lesson, we had to think up a suggestion for a new game feature (part of the feature-creeping we’re doing). Our bank of desks thought that an inventory system would be a nice addition. Funny enough, another group also suggested the same thing, so it’s nice to know many of us are on the same wavelength.

I think I could utilise an inventory system in my game. Something simplistic, maybe where you can only hold a handful of items. I plan on creating a more puzzle-based and/or mystery walking simulator, so to be able to pick up objects and use them further down the line in another room would be extremely useful!

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