Room Game – Unity Pt.3

This week in feature creep all we hear is Radio Ga Ga! If that wasn’t hint enough we added a working music player and a battery to power it. Also new is an updated highlighter script, ray casting to get around the mouse tracking issues we’ve been having, crosshairs and an external animation plugin for tweening. A lot to cover so here we go!

Adding a Radio

Ant provided us with the assets for a radio and a battery to dive straight into the code. We added an audio component to the radio and learnt that by default Unity doesn’t add spatial audio (so will change volume depending on distance from it), one slider set to 1 and that’s ready to go!

The rest of the setup wasn’t too different to having the door script look to see if you had the key in your inventory. Except this time itemName is battery and rather than adding a rotation to the door we tell Unity to turn the volume of the radio to 1 from 0.

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;
}
}
}

We needed a highlighter script that would have multiple text outputs, one to tell us that the radio needs power and once you have the battery for the script to change to tell you the volume toggles on/off. This is achieved by an if/else statement. If the inventory contains battery output x, otherwise output y. Here is the script.

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);
}
}
}

The CrossFadeAlpha was shown to me by Ant because my previous highlighter script was failing to unload the last shown text, causing issues on the UI. This nicely fades text in and out automatically. A touch I’ll definitely be keeping and using from now on in other highlighters.

The whole class had been having issues with mouse pointers not tracking while testing the scene, leaving cursors to drift off out of the game onto other monitors. This became even more of an issue when trying to click on items in the game as up until now we had no way of knowing where the centre of the camera was pointing. Time for some fixes!

We were given a script to incorporate raycasting into the game. This draws a line out from the camera and anything that line hits you can interact with (acts like your mouse pointer). I don’t entirely understand whats going on in the script, I think it’s adding functions that can replace OnMouse Down (OnRayDown) and adding a unit distance for the distance the raycast travels from the camera. Always the issue with being provided a script over having it broken down for you. The important thing is that I understand the functionality. I’ll likely keep a copy of this somewhere incase I need it in future.

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);
}
}
}
}

 

In my mind the best thing today was being given the plugin iTween. This works similar to the tweening animation we’ve been doing in Adobe Animate. Rather than the rotation script we had on the door, we replaced this with iTween and set the vector values again. Now the door smoothly rotates around its axis point over a set amount of frames rather than snapping to a rotation value instantly when opened. Ant mentioned that playing with the vector values could have doors or objects opening/closing in all kinds of directions, once I flesh the scene out I’ll absolutely be using this to make some interesting doors.

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