在Unity C#中创建游戏时间系统
我正在尝试在Unity中为我的游戏制作游戏系统。这是一个使用湖泊图像的2D钓鱼游戏。我想代表鱼“小时活动”行为。我想这样做,如果清晨鱼更活跃,您会得到更多的诱饵,或者如果它的夜间 - 特定类型的鱼类更活跃。另外,我想在时钟命中时做到这一点,让我们说20:00,以便慢慢开始降低Spriterender RGB颜色值。这“暗淡”图片,它使沉浸式变得越来越黑。到23:00时,图片几乎完全是黑暗的。
话虽如此,这就是我在脚本中尝试做的事情。我有一个名为TimeSystem的脚本。在那儿,我有2个属性“小时”和“分钟”,还有2个称为“计时器”的浮点场和小型计时时间可以在游戏分钟中代表。
然后,在更新方法中,IM设置了计时器 - = time.deltatime;然后我开始进行一些检查:
if (timer <= 0)
{
Minute++;
OnMinuteChanged?.Invoke();
if (Minute >= 60)
{
Hour++;
Minute = 0;
if (Hour >= 24)
{
Hour = 0;
}
OnHourChanged?.Invoke();
}
然后设置Timer = MinutEteRealTime;
如果达到60,我会进行这些检查,以使我的分钟返回0,或者将一小时的时间设置为24
。 (不知道这是否完全是正确的方法)
所以这是我的问题。
之后,我进行检查以查看目前的几个小时,并且我试图执行以下操作:
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
基本上将旧的RGB值存储在本地变量中,每个帧都通过特定的数量减少或增加它们(我在示例中使用-0.004F但这只是一个例子。
因此,我从让人开始的游戏开始我的游戏12:00时钟到20:00,然后第一个检查小时的位置&gt; = 20:00,因此它开始使图片变暗。效果很好。然后在早晨,当凌晨5:00时,图片开始慢慢变亮。尝试了它并昨天测试了它,它看起来和感觉很好,一切正常工作……直到我检查以查看第二个周期中会发生什么。因此,在上午5:00之后,PICUTRE变得更加亮,一切都很酷,但是当比赛第二次击中20:00时,什么都没发生。我的代码仅在最初的24小时内执行,而当我将小时重置为0时,它停止工作。.我还附加了一个文本网状,该文本显示了UI中的时钟,并且时间的文本表示是正确的。文本正常工作,应该将分钟数量插入60,然后将它们重置为0,并将+1添加到小时。当敲击时间24时,它们被重置为0,并且相同的过程无关紧要。因此,我希望“图片的颜色更改”能够相应地到达小时,但是它以某种方式仅在第一次工作...它只能工作一次,这就是它。 00第二天,图片颜色不会像预期的那样变暗。
我想注意到,我对编程的新手是,我一直在制作Tictactoe和Pingpong或w/e类似游戏等游戏机应用程序。但是我想加强一点,然后开始使用Unity学习。是的,现在我有一个带有背景的简单场景,带有1个按钮的UI面板在您单击它时显示地图,而时钟的文本网格(似乎非常好,至少可以很好地工作)。现在,我正在努力在游戏中要花费的时间,但我坚持不懈,找不到解决方案。我在网上阅读,可以使用DateTime和.NET中的结构内置的时间板,然后更新每个帧的时间。但是我不知道该怎么做。如果有一种更简单的方法来制定游戏日期和时间系统,请告诉我。我也很想有几秒钟和几年。也许甚至几个月可能会很好,或者我的游戏中建造的整个日历系统。但是..我不知道该如何构建,所以我选择先尝试分钟和小时
。让我知道您是否需要有关我的代码的更多信息,但我认为我涵盖了最重要的内容。
在理想的情况下,我希望我的时钟在游戏中以小时的时间表示:分钟,也许是旁边的文字,说星期一,星期二,星期三。这样我有一天可以在游戏活动中进行您的时间是14:00星期一,我可以介绍一个“锦标赛”,也许是星期五14:00。因此,当它变成星期五14:00 Ingame时,您可以去比赛地点并开始钓鱼比赛。但是,是的,这只是我目前的梦想:D
无论如何都感谢您的时间,祝您有美好的一天! :)
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 indefinetly. 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 ! :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我让它与DateTime一起工作!事实证明,很容易说在我当前的时间上加上(1)并相应地更新。我在一个剧本中获得了50行我想要的一切!我为自己感到骄傲,再次感谢Jay将我指向正确的方向:D
我必须注意的一件重要的事情,关于spritererer.color RGB属性,他们开始默认情况下为1。我以为它们从255开始,我试图降低该价值,但这弄乱了我的代码。相反,您想要的是提取/添加(取决于您的需求)浮动数(例如0到1之间的0.004F),以使图像变亮或变暗::)
只是说,将来遇到任何遇到这个问题的人都可能会发现这种有用:)因此,这个话题得到了解决!
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 G B 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!