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 loafkill · Jun 20, 2013 at 08:10 AM · time.deltatimetorch

Time.DeltaTime question.

Thank you ahead of time for any responses regardless of the outcome.

So I just got through watching some videos, reading some books. All in regards to Unity and Javascript. I jumped in blender made a quick dungeon and some models. Now I get into Unity import my stuff. Throw a character together and start writing its script. Mind you I didn't plan anything and Im a total noob. Anyways to make a long story short... I'm attempting to make a torch, and that torch's life burns out X amount of seconds. Well it goes a bit fast. I've read about Time.DeltaTime but I cant figure it out. I might be tired or just need to take a break. Anyways if someone could let me know how to limit my "torchLightLife" to seconds and not the speed of light!! I would be thankful. See Code below. Oh and If you see anything else wrong let me know lol. IE stuff I shouldn't be doing.

 #pragma strict
 // this is all CharMovement and timing. as well as attacking etc.
 
 
 //Character Movement variables
 var charMove : int;
 var charRotate : int;
 var moveTime : float;
 var rotTime : float;
 
 var allowMove : boolean = true;
 
 
 //Torch controls variables
 var flickerSpeed : float = 0.7;
 var torchLightLife : int = 100;
 var torchExist : boolean = true;
 var randomizer : float;
 function Start () {
 
 }
 
 
 
 function Update () {
 // Movement fwd and back-----------------------------------------------------------------
         if (allowMove)
         {
                 if(Input.GetKey("w"))
                 {
                 transform.Translate(0,0,charMove);
                 print("You Take a step foward");
                 CharacterMove();
                 }
         }
         
         if (allowMove)
         {
                 if(Input.GetKey("s"))
                 {
                 transform.Translate(0,0,-charMove);
                 print("You Take a step back");
                 CharacterMove();
                 }
         }
 // Rotation left and right -------------------------------------------------------------
                         
                         
         if (allowMove)
         {
                 if(Input.GetKey("d"))
                 {
                 transform.Rotate(0,charRotate,0);
                 print("You Turn!");
                 CharactorRot();
                 }
         }
         
         if (allowMove)
         {
                 if(Input.GetKey("a"))
                 {
                 transform.Rotate(0,-charRotate,0);
                 print("You Turn!");
                 CharactorRot();
                 }
         }
         if(torchExist)
         {
             TorchFlicker();
         }
         
 }
 // Character move time pause. This basically stops the player from moving to fast.
 
 function CharacterMove()
 {
     allowMove = false;
     yield WaitForSeconds(moveTime);
     allowMove = true;
 }
 // Character rotation pause. This basically stops the player from rotating to fast.
 
 
 function CharactorRot()
 {
     allowMove = false;
     yield WaitForSeconds(rotTime);
     allowMove = true;
 
 }
 
 
 // This stops the player from clipping through walls. Otherwise with "translate.Transform" i was able to just ignore collision. 
 // I also had to create a tag "cameraCollision" and set it on said obj. 
 function OnTriggerEnter (otherObject:Collider)
 {
 
         if (otherObject.gameObject.tag == ("cameraCollision"))
             {
             
             transform.Translate (0,0,-1);
             allowMove = false;
             print ("Touched a Collider!");
             yield WaitForSeconds(2);
             allowMove = true;
             
             }
             
 
 
 // start torch trigger pick up.
 
         if (otherObject.gameObject.tag == ("torchPickupItem"))
             {
             torchLightLife = (Random.Range(500,5000));
             torchExist = true;
             light.intensity = 2.0;
             Destroy(otherObject);
             print ("Touched a Collider!");
             
             
             }
 
 }
 
 
 
 // Start Torch controls here.
 
 
 function TorchFlicker()
 {
 
     if (torchExist) 
     {
 
         light.intensity = 2.0;
     }
     
     else light.intensity = 2.0;
 
     randomizer = Random.Range (1.1, 2.1);
 
     light.intensity = randomizer;
     
     yield WaitForSeconds (flickerSpeed);
     
     TorchDeath();
 }
 
 function TorchDeath ()
 {
     if (torchLightLife > 0)
     {
         yield WaitForSeconds(1);
         torchLightLife--;
         
     }
     else TorchDie();
 }
 
 function TorchDie()
 {
     if (torchLightLife <= 0)
     {
     light.intensity = 0;
     torchExist = false;
     }
 
 }
 
 
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
0
Best Answer

Answer by Tarlius · Jun 20, 2013 at 12:46 PM

Your problem is that you are calling TorchFlicker every frame. TorchFlicker will will then wait 0.7 seconds, and call TorchDeath, which will then wait 1 second then reduce its life. Therefore, torchLightLife--; will be called every frame (or so) after 1.7s.

A better approach would be to do something more like

 var torchLife : float = 100f; // Note that its a float, not an int
 function ProcessTorch() {
     if(!torchExist) return;
     torchLife -= Time.deltaTime;
     if(torchLife < 0) TorchDie();
 }

And add ProcessTorch to the update. Time.deltaTime is the time in seconds unity took to render the last frame. Since update is called once per frame, you can use deltaTime to track time. You could also use Time.time in a similar way to KiraSensei's suggestion, instead of System.DateTime (which will have higher overheads than a simple float).

Comment
Add comment · Show 2 · 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 loafkill · Jun 20, 2013 at 05:21 PM 0
Share

Ty vm I will try both of ur guys suggestions.

avatar image loafkill · Jun 20, 2013 at 05:41 PM 0
Share

So I tried your suggestion. The torch life no longer moves at the speed of light but isn't exactly second either.

Question: Why is "return" after "if(!torchExist) also I had to change the ! from the parameter. So took it from being if torchExist == false then, to if torchExist == true then - Time.deltaTime. Also what is the purpose of var torchLife :float = 100f; To be more specific what is the purpose of the "f" behind the float?

avatar image
0

Answer by KiraSensei · Jun 20, 2013 at 08:45 AM

For a total noob, your code is quite easily readable, congrats :)

I would suggest you to use System.DateTime like this for example :

 // When you pick up the torch :
 private var startTime:System.DateTime = System.DateTime.Now;

Then you check when you have to destroy the torch :

 // code to put in the Update() method
 var timer:float = (System.DateTime.Now - startTime).TotalSeconds;

 // maxTime contains the "life" value of the torch, in your example : Random.Range(500,5000)
 if (timer < maxTime) torchExist = false;
Comment
Add comment · Show 4 · 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 loafkill · Jun 20, 2013 at 08:49 AM 0
Share

Im a noob and posted a answer ins$$anonymous$$d of responding. Anyways Ty vm.Im way better at modeling/animation but I kinda dig scripting. Its "creative" in a way. I wanted to learn a full coding language but I like to see results, and why do all the hard crap when Unity does it for me? lol. Thanks again.

avatar image KiraSensei · Jun 20, 2013 at 09:21 AM 0
Share

Does it work with that now ?

avatar image loafkill · Jun 20, 2013 at 05:19 PM 0
Share

I haven't attempted yet just woke up. I will here shortly and let you know the results.

avatar image loafkill · Jun 20, 2013 at 05:59 PM 0
Share

I kept getting errors on the if (timer < maxTime). I will attempt to figure it out later. I have stuff I have to do today. Thank you all for responding. I attempted to upvote but it said I didn't have permission.

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

17 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to import the object from server to unity 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 Answers


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