简体   繁体   中英

Creating a game time system in Unity C#

I'm trying to make a game system for my game in Unity.It's a 2D fishing game that uses an image of a lake.You click somewhere to cast your rod and start fishing.I want to have time system in the game mainly because I want to represent fish "hourly activity" behavior. I wanna make it where if its early morning the fish is more active and you get more baits, or if its night time - specific types of fishes are more active. Also, I wanna make it when the clock hit lets say 20:00 for example, to slowly start to decrease the SpriteRenderer RGB color values. That "dims" the picture and it gives the immersion that is getting darker. By the time its 23:00 the picture is almost entirely dark.

That being said here is what im trying to do in my scripts. I have a script called TimeSystem. In there i have 2 properties "Hour" and "Minute" and 2 float fields called "timer" and minuteToRealTime to represent in game minute.

Then, in the Update method im setting timer -= Time.deltaTime; and then i begin some checks:

     if (timer <= 0)
     {
        Minute++;

        OnMinuteChanged?.Invoke();
        if (Minute >= 60)
        {
            Hour++;
            Minute = 0;

            if (Hour >= 24)
            {
                Hour = 0;
                
            }
            OnHourChanged?.Invoke();

        }

and then setting timer = minuteToRealTime;

I do these checks to set my minute back to 0 if it hits 60 or to set the hour back to 0 if it hits 24.

Also, im trying to use Action delegate to fire events at certain hour or minute. (dont know if this is the right approach at all)

So here is my problem.

After that i do a check to see what hour it is currently and im trying to do the following:

if (Hour >= 20 || Hour <= 4) // if the hour is between 20:00 and 04:00 i do the darkening process
            {
                float oldRValue = _gameObject.GetComponent<SpriteRenderer>().color.r;
                float oldGValue = _gameObject.GetComponent<SpriteRenderer>().color.g;
                float oldBValue = _gameObject.GetComponent<SpriteRenderer>().color.b;

                _gameObject.GetComponent<SpriteRenderer>().color = new Color(oldRValue - 0.004f, oldGValue - 0.004f, oldBValue - 0.004f,255);
            }
            else 
            {
               //same thing here but increasing color values so the picture brighens up again until 255,255,255,255 RGB and alpha values
            }
            timer = minuteToRealTime

Basically storing the old RGB values in local variables and each frame decreasing or increasing them by the speciefied amount (i use -0.004f in the example but its just an example. if everything works correctly its easy to balance those numbers with trying different ones so the picture darkening is smoother).

So i start my game from lets say 12:00 clock goes up to 20:00 and then it hits the first check where the hour is >= 20:00 so it begins to darken the picture. Which works perfectly fine. Then in the morning when its 5:00AM the picture starts to brighten up slowly. Tried it and tested it yesterday and it looked and felt nice everything was working accordingly... until i checked to see what will happen in the second cycle. So after 5:00AM the picutre got brighter and everything was cool but when the game hit 20:00 for the second time nothing happened. My code only executes for the first 24 hours and when i reset the hours to 0 it stops working.. I have a textmesh attached also that shows the clock in the UI and the text representation of the time is correct. Text is working as it should it couts the minutes to 60 then resets them to 0 and adds +1 to hour.When the hours hit 24 they are reset to 0 and the same process repeat indef.netly. So i would've expect the "changing of the colors of the picture" to work accordingly to the hour but somehow it only works the first time... It only works one time and thats it.. when the clock hits again 20:00 the next day the picture colors dont get darker as expected.

I wanna note that im fairly new to programming and all, i've been mainly making console apps like TicTacToe and PingPong or w/e similar games. But i wanted to step it up a little and start learning using Unity. So yeah right now I have a simple scene with the background, ui panels with 1 button that shows a map when you click it and a text mesh for the clock(which is working perfectly fine,atleast it seems). Right now im working on the Time that i want to make in my game but im stuck and cant find a solution. I've read online that its possible to use DateTime and Timespan built in structures in .NET and then updating the time each frame.. but i have no idea how to do that. IF there is an easier way to make game date and time system PLEASE let me know. I would love to have seconds and years as well.. maybe even months could be nice, or a whole calendar system built in my game. But.. i have no idea how to build that so i opted to try with Minute and Hour first..

Anyway thats a long wall of text idk if anyone will actually read this since its long AF but i hope to get some help.Please let me know if you need more info about my code but i think that i covered the most important stuff.

In an ideal scenario i would like my clock in game to have Time represented as Hour:minute and maybe a text next to it saying Monday,Tuesday, Wednesday.. That way i could one day make in game events where if for example right now your time is 14:00 Monday i could introduce a "tournament" that starts maybe Friday 14:00. So when it becomes Friday 14:00 ingame you could go to the tournament location and begin the fishing tournament. But yeah thats just in my dreams for now:D

Anyways thank you for you time and have a great day: :)

I got it to work with DateTime. Turned out it was easy as saying AddMinutes(1) to my current time and updating it accordingly: I got all i wanted in one script with 50 lines!I feel proud of myself and thank you again Jay for pointing me to the right direction :D

One important thing I have to note tho, about the SpriteRenderer.Color R GB properties, they start at 1 by default. I thought they start at 255 and i was trying to decrease that value but that messed up my code. Instead what you want is to substract/add (depending on your need) floating numbers like 0.004f between 0 and 1 in order to brighten or darken the image:)

Just saying that for future reference to anyone encountering that problem they might find that useful:) So this topic is resolved!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM