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 Dunsec · Jun 25, 2015 at 03:17 PM · scripting problempausestartsuspend

Suspend script until keypress

Hey there, I've been playing around with this script from here: http://twiik.net/articles/simplest-possible-day-night-cycle-in-unity-5

I'm using the following script in a test environment.

 using UnityEngine;
 using System.Collections;
 
 public class DayNightController : MonoBehaviour {
 
     // The directional light which we manipulate as our sun.
     public Light sun;
 
     // The number of real-world seconds in one full game day.
     // Set this to 86400 for a 24-hour realtime day.
     public float secondsInFullDay = 120f;
 
     // The value we use to calculate the current time of day.
     // Goes from 0 (midnight) through 0.25 (sunrise), 0.5 (midday), 0.75 (sunset) to 1 (midnight).
     // We define ourself what value the sunrise sunrise should be etc., but I thought these 
     // values fit well. And now much of the script are hardcoded to these values.
     [Range(0,1)]
     public float currentTimeOfDay = 0;
 
     // A multiplier other scripts can use to speed up and slow down the passing of time.
     [HideInInspector]
     public float timeMultiplier = 1f;
 
     // Get the initial intensity of the sun so we remember it.
     float sunInitialIntensity;
     void Start() {
         sunInitialIntensity = sun.intensity;
     }
 
     void Update() {
         // Updates the sun's rotation and intensity according to the current time of day.
         UpdateSun();
 
         // This makes currentTimeOfDay go from 0 to 1 in the number of seconds we've specified.
         currentTimeOfDay += (Time.deltaTime / secondsInFullDay) * timeMultiplier;
 
         // If currentTimeOfDay is 1 (midnight) set it to 0 again so we start a new day.
         if (currentTimeOfDay >= 1) {
             currentTimeOfDay = 0;
         }
     }
 
     void UpdateSun() {
         // Rotate the sun 360 degrees around the x-axis according to the current time of day.
         // We subtract 90 degrees from this to make the sun rise at 0.25 instead of 0.
         // I just found that easier to work with.
         // The y-axis determines where on the horizon the sun will rise and set.
         // The z-axis does nothing.
         sun.transform.localRotation = Quaternion.Euler((currentTimeOfDay * 360f) - 90, 170, 0);
 
         // The following determines the sun's intensity according to current time of day.
         // You'll notice I have hardcoded a bunch of values here. They were just the values
         // I felt worked best. This can obviously be made to be user configurable.
         // Also with some more clever code you can have different lengths for the day and
         // night as well.
 
         // The sun is full intensity during the day.
         float intensityMultiplier = 1;
         // Set intensity to 0 during the night night.
         if (currentTimeOfDay <= 0.23f || currentTimeOfDay >= 0.75f) {
             intensityMultiplier = 0;
         }
         // Fade in the sun when it rises.
         else if (currentTimeOfDay <= 0.25f) {
             // 0.02 is the amount of time between sunrise and the time we start fading out
             // the intensity (0.25 - 0.23). By dividing 1 by that value we we get get 50.
             // This tells us that we have to fade in the intensity 50 times faster than the
             // time is passing to be able to go from 0 to 1 intensity in the same amount of
             // time as the currentTimeOfDay variable goes from 0.23 to 0.25. That way we get
             // a perfect fade.
             intensityMultiplier = Mathf.Clamp01((currentTimeOfDay - 0.23f) * (1 / 0.02f));
         }
         // And fade it out when it sets.
         else if (currentTimeOfDay >= 0.73f) {
             intensityMultiplier = Mathf.Clamp01(1 - ((currentTimeOfDay - 0.73f) * (1 / 0.02f)));
         }
 
         // Multiply the intensity of the sun according to the time of day.
         sun.intensity = sunInitialIntensity * intensityMultiplier;
     }
 }

How could I suspend this script so that it will only start after a keypress?

At the moment, the moment the scene starts the script will start. How would I make this so the sun will only start revolving after the user has input a keypress?

Thanks for your time :D Dun

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
Best Answer

Answer by troien · Jun 25, 2015 at 03:55 PM

You could use the enabled property that every monobehaviour has. When it is false, Update won't be called.

This is how you could implement it using Start as a Coroutine:

 using UnityEngine;
 using System.Collections;
 
 public class Waiting : MonoBehaviour
 {
     private IEnumerator Start()
     {
         this.enabled = false; // Disable us while waiting
 
         // Wait for input
         while (!Input.anyKey)
         {
             yield return null;
         }
 
         this.enabled = true; // Enable us now
     }
 
     void Update ()
     {
         Debug.Log("Updating");
     }
 }


Comment
Add comment · Show 1 · 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 Dunsec · Jun 25, 2015 at 04:30 PM 0
Share

Thank you for this, had a good read on Coroutine's and implemented this else where.

avatar image
1

Answer by The_Guy · Jun 25, 2015 at 04:26 PM

In the first line of your Update() function you could write something like:

 if(Input.GetKeyDown(KeyCode.Enter)) startRotating = true;
 if(!startRotating) return;

and have bool startRotating = false; at the top.

Comment
Add comment · Show 1 · 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 Dunsec · Jun 25, 2015 at 04:32 PM 0
Share

I was so close when I was testing my own ways. Thank you helped me see where I went wrong. Two correct answers only 1 tick allowed.

Thanks!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Pause game on Start doesn't quite work 1 Answer

Timescale wont work 0 Answers

AudioSource glitching when resuming from suspend on iOS 0 Answers

A Proper Way To Pause A Game 9 Answers

"Restart " C# script that has [ExecuteInEditMode] attribute and Start() function. 0 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