Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Matt 5 · Feb 15, 2012 at 08:14 PM · timerlightsracing

Race begins when Green light shows

Hey Everyone,

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by worldofcars · Feb 15, 2012 at 09:36 PM

 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);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by ZweiD · Feb 15, 2012 at 09:59 PM

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]

(assuming you store the textures in an array)

Hope that helps.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I make a lap timer? 2 Answers

Starting timer when I press a key 1 Answer

How can i add a timer to this code 1 Answer

How to make a timer read to the .001 of a second 2 Answers

Can anyone help me figure this script out? (Light Trigger) 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges