Search Unity

Returning True and False at the same time | Need Help | C#

Discussion in 'Scripting' started by Triplequestionmark, Dec 24, 2018.

  1. Triplequestionmark

    Triplequestionmark

    Joined:
    Sep 25, 2018
    Posts:
    3
    Hi! I'm making a script for a simple day/night cycle using a directional light's Euler Angles. Now, how Euler Angles work for the directional light is the following:

    0° (Dawn) > 45° (Morning) > 90° (Noon) > 45° (Evening) > 0° (Dusk) > 360° (Start of Night) > 270° (Midnight) > 360° (End of Night) > 0° (Dawn)

    So, the only way to tell whether it's Dawn or Dusk is by checking if the Euler Angles are increasing or decreasing.
    Ex. If the Euler Angles < 10 and they are increasing, then it's Dawn. If they are decreasing, then it's Dusk.

    I made the following script to check if the Euler Angles are increasing or decreasing:

    Code (CSharp):
    1. private float sunEulerAnglesX;
    2. public float sunMovementSpeed;
    3.  
    4. void Update () {
    5.                 //sun refers to the Directional Light
    6.         sunEulerAnglesX = sun.transform.localEulerAngles.x;
    7.         sun.Rotate (Vector3.right * Time.deltaTime * sunMovementSpeed);
    8.                
    9.                 print(isIncreasing());
    10. }
    11.  
    12. void FixedUpdate(){
    13.         StartCoroutine (newOldVal ());
    14. }
    15.  
    16. IEnumerator newOldVal(){
    17.         oldVal = sunEulerAnglesX;
    18.         yield return new WaitForSeconds (1);
    19.         newVal = sunEulerAnglesX;
    20. }
    21.  
    22. bool isIncreasing(){
    23.     if (oldVal < newVal) {
    24.         return true;
    25.     } else {
    26.         return false;
    27.     }
    28. }
    After around 30 seconds of running, isIncreasing() returns about 500 falses and 100 trues when it should only be false (vice versa happens when it should only be true). I'm thinking that the 100 trues are caused when oldVal == newVal.

    Does anyone know what is happening for sure? And if it is because of the oldVal == newVal from time-to-time, does anyone know how I could fix it?
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,408
    try printing out the values with Debug.Log(),
    probably it returns values in different range, instead of 0-360
     
  3. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    Just wonder, wouldn't be better having simply ranges of angles check, rather checking if is increasing, or decreasing?

    I understand, your 360 deg cycle represents 24h clock.
    If that is the case, you only need check ranges.

    Regarding issue, as mentioned, it may happen, that your angles range is beyond expected. You maybe see negative angles and positive angles, greater than 360. Which in result could make you stuck at 360 result or 0. One way you could deal wit, if the case, is use Mathf.mod
     
  4. Triplequestionmark

    Triplequestionmark

    Joined:
    Sep 25, 2018
    Posts:
    3
    It returns the same.
     
  5. Triplequestionmark

    Triplequestionmark

    Joined:
    Sep 25, 2018
    Posts:
    3
    I'll look into the ranges of angle checks, and I'll give an update.
     
  6. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Don't start coroutines like that, it will drain your performance like crazy and might cause side-effects you're encountering right now.

    If you want something to update the code constantly. Start a single coroutine instance (e.g. in Start) and run your coroutine code in while loop. Don't forget the yield though.
     
    Antypodish likes this.
  7. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,769
    And in fact, best if you avoid coroutines. These are not magic tools, if you don't understand how they work.