Room Game in Unity (part 2)

Continuing with our game development in Unity, we added some more features to our walking simulator: a radio that we’ll script to need a battery to turn on/off, change our mouse to raycasting, and use a pre-made script to animate our door swinging open, instead of it just jumping open when the key is used on it.

A lot of the code we have used in previous classes can be used again, just with a few minor changes. For example, we coded that our door needed a specific key to open it, which is similar to our radio needing a battery to power it. First, we had to bring in the models Ant provided for the radio and the battery, place them in our room, then add audio to the radio.

audio-component
The component for the audio of the radio.

We found out that Unity doesn’t automatically have spatial audio, so the room would be filled with the same level of music no matter where the player stood. This is easily changed in the component by changing the Spatial Blend to 1, along with reducing the volume to 0 so the radio is in an off state until the player picks up the battery and then tries to turn it on.

The code for using the battery with the radio was quite close to what we’d done for the door and key:

public class RadioController : MonoBehaviour
{

public AudioSource source;
public string keyItemName;
public string myName1;
public string myName2;

void OnMouseDown ()
{
// Store the distance from this key to the main camera
float distance = Vector3.Distance (transform.position, Camera.main.transform.position);

// If the distance is less than 3 units…
if (distance < 3) {
Inventory inventory = GameObject.FindObjectOfType<Inventory> ();
if (inventory.items.Contains (“Battery”) == true) {
source.volume = 1 – source.volume;
}
}
}

It helped having a functioning inventory already in my game, so we didn’t need to write a lot of new code to get this to work. We were then given the option to try writing the code we’d need for adding a custom UI text, where it would start out saying “Radio – requires a battery” and change to “Radio – click to toggle volume”. I tried my hand at it and this was the result:

void OnMouseOver ()
{
Inventory inventory = GameObject.FindObjectOfType<Inventory> ();

if (inventory.items.Contains (“Battery”) == true) {
GameObject.FindObjectOfType<Text> ().text = myName2;
GameObject.FindObjectOfType<Text> ().CrossFadeAlpha (1, 0, true);
GameObject.FindObjectOfType<Text> ().CrossFadeAlpha (0, 1, true);

} else {
GameObject.FindObjectOfType<Text> ().text = myName1;
GameObject.FindObjectOfType<Text> ().CrossFadeAlpha (1, 0, true);
GameObject.FindObjectOfType<Text> ().CrossFadeAlpha (0, 1, true);
}
}
}

Again, this took bits and pieces of script we’d already learned  for assets currently in our game and altered them slightly. I had some errors with brackets that took a little time to comb through; it was useful that Ant reminded me to edit the formatting of my scripts, so that everything would line up correctly and you could see if you missed a curly bracket or semi-colon somewhere. Unity will also have an easier time reading the script. It will be interesting to think of other ways we can apply this code in our games.

I was also shown a way to have the text of the highlighter script fade in and out with the CrossFadeAlpha transition. It adds a nice touch to the game and I’ll likely continue to use it.

I continued onwards with my tasks, turning my attention to incorporating raycasting in the game, rather than relying on the mouse pointer. I like this idea, although I felt compelled to come up with a crosshairs in the middle of the screen, otherwise it would become too difficult to guess where the raycaster was pointer. It’s quite a short script, but it doesn’t feel very straightforward. I believe it is telling Unity to replace the mouse with a draw line and centre it on the screen, with a distance of 5 in this case, as well as replacing the hovering-over and mouse down options.

public class RayCaster : MonoBehaviour
{

void Update ()
{
//Debug.DrawLine (transform.position, transform.position + transform.forward * 5);
RaycastHit hit;
if (Physics.Raycast (transform.position, transform.forward, out hit, 5)) {
GameObject objectHit = hit.collider.gameObject;
if (Input.GetMouseButtonDown (0)) {
objectHit.SendMessage (“OnRayDown”, SendMessageOptions.DontRequireReceiver);
} else {
objectHit.SendMessage (“OnRayOver”, SendMessageOptions.DontRequireReceiver);
}
}
}
}

This did mean that I had to change all of the OnMouseOver  and OnMouseDown in my various scripts with their OnRayOver and OnRayDown equivalents. However, the overall result is something I quite like while in game:

crosshairs

You can see the radio with a battery next to it on the bookshelf, the flashlight is on from the last lesson with the key still waiting on the table, and the crosshair is in the middle of the screen to denote where the raycaster line is pointing.

Finally, in the last part of the lesson, we learned about animating in Unity with a script called iTween. All we needed to do was create the script in our project, and alter our DoorController script to use the animation:

public class DoorController : MonoBehaviour
{

// The name of the object that controls this door
public string keyItemName;
public GameObject door;

void OnRayDown ()
{
// Find the InventoryController
Inventory inventory = GameObject.FindObjectOfType<Inventory> ();
//Does the inventory list contain the key item?
if (inventory.items.Contains (“Key”) == true) {
//Rotate this door
iTween.RotateBy (door, new Vector3 (0, -0.25f, 0), 10);
//Destroy this script
Destroy (this);
}
}
}

This script will look nearly identical to the last time I posted it, however we are now using the iTween script to cause the door to rotate outwards once the player uses the key on it. I think the animation looks really good and adds more “realism” to what (so far) is a very simple room. I’m going to see if I can use this in other ways, perhaps opening secret doors or having hidden compartments open to reveal items needed to solve puzzles. It will just be an added feature to my game. I’m also going to search or some other “simpler” scripts I can incorporate into the game to achieve a puzzle walking sim.

While it may not look like much right now, I really do feel like an idea is coming together for my game – I think we’ve learned a lot that I can utilise in other situations than the ones we’ve been shown. I’m going to continue working on my game and will post again once I have worked more on it (possibly adding some new rooms and getting some puzzles going).

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