- Home /
Nothing should be seen without the flashlight pointing at it.
I'm trying to make a game working with the idea of echolocation. We are connecting a mic to Unity and sounds will enable the flashlight to turn on and off. The volume and duration of the sound will determine the intensity and range of the light. We've created a black environment with white outlines so that even when the light shines on it all you can really see are the edges of our objects and environments. So far so good. The issue we seem to be running into is that even with a black terrain, a black skybox and no other light sources, the outlines can still be seen even when the light is turned off. We've tried completely turning down the ambient light and jacking up the intensity of our spotlight, but when we do that nothing can be seen whatsoever, even with the spotlight. Are we just missing something, or is there something special we need to do to solve this issue?
My programmer also just mentioned he's having issues with the script he's using. The script is having difficulty getting access to the variable of the light. If anyone has any answers for that as well it'd be much appreciated!
using UnityEngine;
using System.Collections;
public class Audio : MonoBehaviour
{
Light l;//Need to determine how this will be accessed
GameObject o;
int qSamples = 256;
float refValue = 0.1F;
float rmsValue;
float dbValue;
float pitchValue;
private float[] sample; // audio samples
private float[] spectrum; // audio spectrum
void Start ()
{
// o = //UnityEngine.GameObject.FindGameObjectWithTag("First //Person Controller");
l = (Light)(o.GetComponent("Light"));
audio.clip = Microphone.Start("Built-in Microphone", true, 1,44100);
audio.Play();
sample = new float[qSamples];
spectrum = new float[qSamples];
}
void Update ()
{
if (audio.isPlaying)
{}
else if (audio.clip.isReadyToPlay)
{
audio.Play();
l.enabled = true;
}
else
{
audio.clip = Microphone.Start ("Built-in Microphone", true, 10,44100);
}
//FIGURE OUT ALGORITHM FOR AMOUNT OF LIGHT AND RANGE
//float y = audio.GetSpectrumData(128,0,FFTWindow.BlackmanHarris)[64]*1000000;
AnalyzeSound();
if (dbValue < 5)
{
l.enabled = false;
}
else if (dbValue < 30)
{
l.range = 9;
}
else if (dbValue < 60)
{
l.range = 30;
}
else if (dbValue < 100)
{
l.range = 80;
}
}
void AnalyzeSound()
{
audio.GetOutputData(sample, 0); // fill array with samples
int i;
float sum = 0;
for (i=0; i < qSamples; i++)
{
sum += sample[i]*sample[i]; // sum squared samples
}
rmsValue = Mathf.Sqrt(sum/qSamples); // rms = square root of average
dbValue = 20*Mathf.Log10(rmsValue/refValue); // calculate dB
if (dbValue < -160) dbValue = -160; // clamp it to -160dB min
}
}
The script is attached to a First Person Controller and the Spot Light is attached to the First Person Controller too. There is a diffuse shader being used.
What shader is assigned to the environment material? And what's the code in the script that is having "difficulty"?
Answer by robertbu · May 20, 2014 at 05:14 PM
Usually when questions similar to yours come across the list the answer is changing the settings of Ambient Light:
Edit > Render Settings > Ambient Light
Change the color to black for this setting.
As for the second issue, you'll need to programmer to post a new question explaining in more detail. I see one potential thing you might want to improve. Use the generic version of GetComponent() instead of the string version.
l = o.GetComponent<Light>();
But what he is doing should not cause problems as long as 'o' has a light component.
Your answer
Follow this Question
Related Questions
Flashlight battery script 3 Answers
Flash light off on start 1 Answer
How do I get my flashlight to work? 1 Answer
Flashlight Flicker! 2 Answers