I have made a racing game and I want to create a traffic light model with lighting effects that starts the race so it would go Red, 2 yellows then Green and the race would begin.
Now coding wise I have an idea but I have a feeling I’m going about it the wrong way
I have something like this in mind.
var redTexture : Texture2D;
var yellow1Texture : Texture2D;
var yellow2Texture : Texture2D;
var greenTexture : Texture2D;
var currentTime : int[10];
then base the switch on the light animations based on a timer of 10 seconds.
could anyone help me with this on how to set up that kind of timer.
Since I don’t know what language you use, I am just going to give you some pseudo code (that looks a lot like Boo) to give an idea on the general logic:
def Update():
currentTime += Time.deltaTime
if currentTime < 10:
// show redTexture
else if currentTime < 20:
// show yellow1Texture
else if currentTime < 30:
// show yellow2Texture
else if currentTime < 40:
// show greenTexture
else:
// maybe destroy the timer object?
That is probably the easiest way of achieving the behaviour you want.
Alternatively you could check whenever the currentTime is greater or equal to 10s and change to the next texture.
If you do it that way, you would need to keep track of the current state:
def Update():
currentTime += Time.deltaTime
if currentTime > 10:
currentTime -= 10
if currentState < 4: // assuming the green state is state 4
currentState += 1
// switch texture to lightTexture[currentState]
var car : GameObject;
var redTexture : Texture2D;
var yellow1Texture : Texture2D;
var yellow2Texture : Texture2D;
var greenTexture : Texture2D;
private var startTime;
private var restSeconds : int;
private var roundedRestSeconds : int;
private var displaySeconds : int;
private var displayMinutes : int;
var countDownSeconds : int;
function Awake() {
startTime = Time.time;
}
function OnGUI () {
//make sure that your time is based on when this script was first called
//instead of when your game started
var guiTime = Time.time - startTime;
restSeconds = countDownSeconds - (guiTime);
//display messages or whatever here -->do stuff based on your timer
if (restSeconds == 3) {
GUI.Box(Rect(200,200,200,200),redTexture);
}
if (restSeconds == 2) {
GUI.Box(Rect(200,200,200,200),yellow1Texture);
}
if (restSeconds == 1) {
GUI.Box(Rect(200,200,200,200),yellow2Texture);
}
if (restSeconds == 0) {
GUI.Box(Rect(200,200,200,200),greenTexture);
//start your game here
//enable all scripts to start your game... EXAMPLE:
car.GetComponent("Driving").enabled = true;
}
//display the timer
roundedRestSeconds = Mathf.CeilToInt(restSeconds);
displaySeconds = roundedRestSeconds % 60;
displayMinutes = roundedRestSeconds / 60;
text = String.Format ("{0:00}:{1:00}", displayMinutes, displaySeconds);
GUI.Label (Rect (400, 25, 100, 30), text);
}