- Home /
Can anyone help me figure this script out? (Light Trigger)
Hello,
So I am currently working on a Science Fictional personal project and I am working on a scene where the player would enter a trigger and then the lights would turn on individually/in pairs of two, so basically a timer.. Let me give you a better example using the demonstration of the 3D model I am working on.
Example:
It is a very tacky fast rushed example, but I hope you know what I'm trying to achieve from this. Now I could spent time and time and most likely not get this done.. I'm looking for someone who is professional in coding.
If you feel you can do this, that would be great.
The script would run like Player enters trigger >>> Both side lights play in pairs of 2 every 0.2 seconds working its way up the tunne, as well as playing sounds from each one.
Issue #1: You appear to be looking for a professional to code this for you, which is not what this list is about. You may want to look at the collaboration are a of Unity Forums to find someone to partner with. Issue #2: If you are trying to do this with real lights, and that is an awful lot of pixel lights. Unity supports 4 by default. You need 40 for the drawing above.
This is not a hard problem. There are a couple of different ways to approach the problem partly depending on what language you code in.
Alright thank you for the advice buddy, but I appreciate that you looked in to this. Please email me alternatively if you come across anyway who is efficient in JS/C*, and want to get involved in a Sci-Fi Horror based game. Email: Dzangana@hotmail.co.uk, find my project at www.DimaZangana.wordpress.com and head to the Unity Game Development page.
Answer by nesis · Jan 15, 2014 at 05:40 AM
Make a C# script, attach it to a trigger, and add this code just above the } at the bottom of the autogenerated file:
public Light[] leftlights; //assign all your left lights
public Light[] rightlights; //assign all your right lights
private int currentLight = 0;
public float initialDelay = 0.1f;
public float delayBetweenLights = 0.4f;
public void Start() {
//turn off all lights to start with
for (int i=0; i<leftlights.Length; i++) {
leftlights[i].enabled = false;
rightlights[i].enabled = false;
}
}
public void OnTriggerEnter(Collider other) {
//the tricky part: using InvokeRepeating() to do something over and over over time...
InvokeRepeating("TurnLightsOn",initialDelay,delayBetweenLights);
}
public void TurnLightsOn() {
//assumes leftlights and rightlights have the same number of elements
if (currentLight>leftlights.Length-1) return; //early out if we've lit all the lights
//enable the lights we're currently up to in the arrays
leftlights[currentlight].enabled = true;
rightlights[currentlight].enabled = true;
//increment currentLight so next time this method gets called, we're accessing the next light in each array
currentLight++;
//we've lit all the lights, so stop calling this method
if (currentLight>leftlights.Length-1) CancelInvoke("TurnLightsOn");
}