• 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 /
  • Help Room /
avatar image
0
Question by chon59 · Jul 06, 2016 at 12:11 PM · timetime.deltatimeseconds

Time.deltaTIme does not work the way it should.

Hello everyone !

I'm trying to make a texture blend to one another in 5 sec when I press space. I have found a shader that works well on the forum everything goes the way I want but when I'm trying to update it in 5 sec with that code :

 var timeLeft = 0.000;
 var duration = 0.000;
 function Start () {
 
 
 }
 
 function Update () {
  
     if (Input.GetKeyDown("space")) {
      //   print("space key was pressed in Update");
 
    
       
         while (duration <= 5) {
             timeLeft += Time.deltaTime;
             duration += Time.deltaTime;
           //  print(timeLeft);
            // print(duration);
             var lerp = Mathf.PingPong (timeLeft, duration) / duration;
             GetComponent.<Renderer>().material.SetFloat( "_Blend", lerp );
 
         }
 
     }
 
     else if (duration >= 5) {
 
         duration = 0;
     }
         
 }

It just change my texture in one frame and I don't know why, shouldn't it be doing it in 5sec (as duration has +Time.deltaTime everyframe?).

Thanks in advance

Comment
Add comment · Show 2
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 Dibbie · Jul 06, 2016 at 12:34 PM 0
Share

You should never add a while loop (or any loop) in Update, because it runs every frame, and loops can take more than 1 frame to complete, so in general, doing it that way seems to me like a bad idea.

Also, Time.deltatime really only represents - as far as im aware the "second" equivilent based on the frames the game is running at or settings its being played at, so you may need to do duration += 1 * Time.deltaTime, saying more "add 1, over the course of 1 delta time second".

avatar image chon59 Dibbie · Jul 06, 2016 at 01:13 PM 0
Share

Hi Dibbie first thanks for this fast answer ! I've read that I shouldn't be using a loop inside Update $$anonymous$$aybe I should be using a coroutine? Never tried that don't really know how to do it yet.

Then I've tried to put duration += 1 * Time.deltaTime but nothing changes, I don't get why :(

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by chon59 · Jul 06, 2016 at 03:08 PM

I've achieved this by doing a coroutine in C#

here it is if someone needs it :

 using UnityEngine;
 using System.Collections;
 
 public class coroutine : MonoBehaviour {
     float timeLeft = 0;
     float duration = 0;
     float blending = 5;
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetKeyDown("space"))
         {
             if (duration >= 5)
             {
                 duration = 0;
                 StartCoroutine("corout");
             }
             else
             {
                 StartCoroutine("corout");
             }
          }
        
     }
 
     IEnumerator corout()
     {
         while (duration <= 5)
         {
             timeLeft += Time.deltaTime;
             duration += Time.deltaTime;
             print("timeLeft" + timeLeft);
             print("duration" + duration);
             var lerp = Mathf.PingPong(timeLeft, blending) / blending;
             GetComponent<Renderer>().material.SetFloat("_Blend", lerp);
             yield return null;
         }
         
     }
 
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

61 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I do math in unity.(timer/60 and timer%60) 1 Answer

Animation window time bar shows 1 minute increments - HELP 0 Answers

How to Make Timer (Score) Count Up Faster? 1 Answer

Wont instantiate game object 1 Answer

Milliseconds Timer Question 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