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 /
This question was closed Oct 21, 2021 at 04:51 PM by IFixGames for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by IFixGames · Oct 21, 2021 at 02:57 PM · calculation

Unity Calculation fails

I have the following problem that I am despairing of, and that is I have a simple calculation which for whatever reason does not give the correct result. My script looks like this:

 using UnityEngine;
 using System;
 using UnityEngine.UI;
 using UnityEngine.Advertisements;
 
 public class OfflineIncome : MonoBehaviour, IUnityAdsListener
 {
     public Items OfflineItem;
     public Clicker clicker;
     public Save save;
     public Rps rps;
 
     public int MaxOfflineTime = 7200;
     public float CurrentOfflineTime;
     public float OfflineEarned;
     public float AwayFor;
     public float RewardedFor;
 
     public Text OfflineEarnedText;
     public Text AwayForText;
 
     public Button TakeButton;
     public Button DoubleButton;
 
     public GameObject OfflinePanel;
     public GameObject AdStatusPanel;
 
     public Button AcceptAdStatus;
     public Text AdStatus;
     public Text AdStatusText;
 
     DateTime currentTime;
     DateTime endTime;
 
     public double seconds;
 
     public float days;
     public float hours;
     public float minutes;
     public float secs;
 
 #if UNITY_IOS
     private string gameId = "4124812";
 #elif UNITY_ANDROID
     private string gameId = "4124813";
 #endif
 
     bool testMode = true;
     public string myPlacementId = "rewardedVideo";
 
     private void Start()
     {
         Advertisement.Initialize(gameId, testMode);
         Advertisement.AddListener(this);
 
         DoubleButton.GetComponent<Button>();
         DoubleButton.interactable = Advertisement.IsReady(myPlacementId);
         if (DoubleButton) DoubleButton.onClick.AddListener(ShowRewardedVideo);
     }
 
     void ShowRewardedVideo()
     {
         Advertisement.Show(myPlacementId);
     }
 
     public void OnUnityAdsReady(string placementId)
     {
         if (placementId == myPlacementId)
         {
             DoubleButton.interactable = true;
         }
     }
 
     public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
     {
         if (showResult == ShowResult.Finished)
         {
             OfflineEarned *= 2;
             clicker.Radiation += OfflineEarned;
             AdStatusPanel.SetActive(true);
             AdStatus.text = "Ad finished successfully";
             AdStatusText.text = "You earned: " + OfflineEarned.ToString("F2") + " Radiation";
         }
         else if (showResult == ShowResult.Skipped)
         {
             //Kein Reward
         }
         else if (showResult == ShowResult.Failed)
         {
             AdStatusPanel.SetActive(true);
             AdStatus.text = "Ad failed";
             AdStatusText.text = "The ad did not finish due to an error!";
         }
     }
 
     public void OnUnityAdsDidError(string message)
     {
 
     }
 
     public void OnUnityAdsDidStart(string placementId)
     {
 
     }
 
     public void Accept()
     {
         AdStatusPanel.SetActive(false);
     }
 
     public void Take()
     {
         clicker.Radiation += OfflineEarned;
         OfflinePanel.SetActive(false);
     }
 
     public void Started()
     {
         //rps.CalculateRadiationPerSecond();
         OfflinePanel.SetActive(true);
         currentTime = System.DateTime.Now;
         long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
         endTime = DateTime.FromBinary(temp);
         TimeSpan difference = currentTime.Subtract(endTime);
         //string TimeText = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D2}", difference.Days, difference.Hours, difference.Minutes, difference.Seconds);
 
         seconds = difference.TotalSeconds;
 
         days = Convert.ToSingle(difference.Days);
         hours = Convert.ToSingle(difference.Hours);
         minutes = Convert.ToSingle(difference.Minutes);
         secs = Convert.ToSingle(difference.Seconds);
 
         if (seconds >= MaxOfflineTime)
         {
             //For testing 
             Debug.Log("Offlinetime > MaxOfflineTime");
             Debug.Log("MaxOfflineTime: " + MaxOfflineTime);
             Debug.Log("Rp/s: " + rps.RadiationPerSecond);
             Debug.Log("OfflineIncome: " + OfflineItem.OfflineIncome);
             Debug.Log("OfflineEarnedBefore: " + OfflineEarned);
 
             OfflineEarned = (MaxOfflineTime * OfflineItem.OfflineMultiplier) * rps.RadiationPerSecond;
 
             Debug.Log("OfflineEarnedAfter: " + OfflineEarned);
         }
         else if (seconds < MaxOfflineTime)
         {
             //For testing
             Debug.Log("OfflineTime < MaxOfflineTime");
             Debug.Log("seconds: " + seconds);
             Debug.Log("Rp/s: " + rps.RadiationPerSecond);
             Debug.Log("OfflineIncome: " + OfflineItem.OfflineIncome);
 
             OfflineEarned = (float)seconds * (rps.RadiationPerSecond * OfflineItem.OfflineIncome);
         }
 
         if(days > 0)
         {
             AwayForText.text = days + "d " + hours + "h " + minutes + "m " + secs + "s";
         }
         else if( hours > 0)
         {
             AwayForText.text = hours + "h " + minutes + "m " + secs + "s";
         }
         else if (minutes > 0)
         {
             AwayForText.text = minutes + "m " + secs + "s";
         }
         else if (secs> 0)
         {
             AwayForText.text = secs + "s";
         }
 
         OfflineEarnedText.text = OfflineEarned.ToString("F1") + " Radiation";
         //AwayForText.text = TimeText;
     }
 
     public void Double()
     {
         OfflinePanel.SetActive(false);
     }
 
     private void OnApplicationQuit()
     {
         PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());
         Debug.Log("Saving this time: " + System.DateTime.Now);
     }
 
     private void OnApplicationPause(bool pause)
     {
         if (pause)
         {
             PlayerPrefs.SetString("sysString", 
             System.DateTime.Now.ToBinary().ToString());
             Debug.Log("Saving this time: " + System.DateTime.Now);
         }
     }
 }
 

The error is in the calculation of offlineincome once seconds > Maxofflinetime. The calculation should be as follows according to the console output: OfflineEarned = (MaxOfflineTime(7200) OfflineItem.OfflineMultiplier(1)) rps.RadiationPerSecond(1); However, the result is always 18000, which makes no sense in my eyes. I will also add a screenshot of the console. alt text

screenshot-2021-10-21-195646.png (24.0 kB)
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Oct 21, 2021 at 03:18 PM

Are you sure that your two formulas are right? If the time is below the max time you actually multiply by "OfflineIncome". However when you are above the max time you multiply by "OfflineMultiplier" instead.


This seems a bit weird. If you just want to have a max time, why don't you just clamp the seconds?

You haven't printed out the value of "OfflineMultiplier" only "OfflineIncome", so I guess you used the wrong variable?


As I said it would be much easier to use the same calculation and just clamp the seconds

 if (seconds > MaxOfflineTime)
 {
     seconds = MaxOfflineTime;
 }
 // do your calculation here using seconds as usual.

Of course you can also clamp the seconds like this as well

 seconds = Mathf.Min(difference.TotalSeconds, MaxOfflineTime);

or

 seconds = Mathf.Clamp(difference.TotalSeconds, 0, MaxOfflineTime);
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

Follow this Question

Answers Answers and Comments

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

Related Questions

GUI error calculating 2 Answers

Math Genius NEEDED! Algorithm problem with parabola 1 Answer

Hit box based off of a parabola 1 Answer

Mathf.Lerp like User Define Function 2 Answers

Please help me to find a better way to calculate prices. 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