Importing an FBX

We downloaded the Gem Detector FBX from from our Teams channel and imported it into Unity, in a new folder called ‘Models’. In the Inspector, we used the “Extract Textures” and “Extract Materials” options to take the textures and materials stored inside the FBX and place them into our project as items we could edit. When extracting the textures, Unity warned that two were Normal Maps and offered to mark them as such for us, which we accepted.
What is a Normal Map?
A normal map is just a texture file. It is used to cheaply, from the point of view of rendering time, provide fine detail in models. The little video below illustrates this. There are two square plates, side-by-side.
The plate on the left has been modelled to have a circular feature that rises up in a lip and then back down again. It uses many vertices and polygons to model this.
The plate on the right is just a single quad, but it has a normal map applied to it. How was this normal map created? It was “baked” from the plate on the left. This plate is always flat, but it reacts to light as if it isn’t.
See how they both behave very similarly when a light is passed over them. It isn’t until the camera tilts down to a very low angle that the illusion breaks and the plate on the right starts to look a little odd. The video shows the wireframes at the end so you can see the difference in the two plates from a geometry standpoint.
Adding A Light and Sound To Our Detector
To light up the detector, we need two elements. A point light to cast light on the rest of the model and an emissive material we can switch to to make the glass cover on the bulb look lit-up.
With our extracted materials and textures, we now had a material called ‘Light Cover’ in our ‘Models’ folder. This is used for the lamp cover at the top of the sensor. It’s supposed to be transparent, so we selected that material and in the Inspector changed the Rendering Mode setting from Opaque to Transparent and we then opened the Albedo colour and reduced the A value (which stands for Alpha and is the degree of transparency) down to about 175.

We then duplicated this material and renamed this copy as ‘Light Cover (On)’. We edited this one to enable emission by clicking on the Emission check box, then opening the Emission Colour dialog and setting the colour to a red colour and finally pressing +2 at the bottom of that dialog once to brighten the colour.
Then we added a new object, a point light, as a child of the “Light Cover” object in the detector and manoeuvred it into position.
Finally, we downloaded a sound file, of a beep sound, from our Teams channel to an ‘Audio’ folder in our project and set it as the AudioClip for an AudioSource component, with Play On Wake disabled, attached to our Light Cover object.
Script for Turning on the Light and Sounding A Beep
Here is the code for turning on and off the light and swapping the material on the light cover:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SensorLight : MonoBehaviour
{
public Material MaterialOff;
public Material MaterialOn;
private bool _isOn = false;
private MeshRenderer _mr;
private AudioSource _as;
private Light _bulb;
// Start is called before the first frame update
void Start()
{
_mr = GetComponent<MeshRenderer>();
_as = GetComponent<AudioSource>();
_bulb = GetComponentInChildren<Light>();
}
// Update is called once per frame
void Update()
{
if (_isOn)
{
_mr.material = MaterialOn;
_bulb.enabled = true;
}
else
{
_mr.material = MaterialOff;
_bulb.enabled = false;
}
}
public void TurnOn()
{
_as.Play();
_isOn = true;
}
public void TurnOff()
{
_isOn = false;
}
}
Note that it uses GetComponent<T>() and GetComponentInChildren<T>() to find the different components we need to interact with, rather than having them as public property we’d have to set manually. This approach can be a little easier to maintain.
Connecting it all to the Gem Sensor
We downloaded a finished version of the Gem Sensor ProximitySensor.cs script from our Teams channel. Very much based on last week’s code, but with the addition of logic to work out the flashing light’s timing.
The final thing was a very simple class to tie the ProximitySensor and the ItemScatterer together:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LinkScatterToSensor : MonoBehaviour
{
public ItemScatterer ItemScatter;
public ProximitySensor ProximitySensor;
// Update is called once per frame
void Update()
{
if (ItemScatter.nearestItem != null)
{
ProximitySensor.Target = ItemScatter.nearestItem.transform;
}
}
}
Code Download
The code for this week’s project is on our GitHub, as always.