- Home /
Real time and slider values
Hey guy's
So I've recently came back to unity after a few years and I've been given the task of making a tamagotchi type of game.
I'm currently working on a feeding system using real time and sliders to show the player how hungry the animal is. But I'm finding it difficult to change the slider value. basically I want the slider value to change taking the last feeding time and the next feeding time into consideration.
Once the player feeds the animal, My code saves the current time using System.DateTime.Now and saves a second time using the current time with .AddHours(4).
As an example, If the player feeds the animal at exactly 12 o'clock mid day. The next feeding time would be exactly 4 o'clock. If the player opens the game at 2 o'clock, the slider should be half filled.
I've tried approaching this using a few methods but haven't had any luck.
any help would be greatly appreciated.
Answer by myzzie · Nov 18, 2020 at 03:57 PM
var startTime = DateTime.Now; //When you fed it
var halfWay = startTime.AddHours(2); //when you check in on it after 2 hours
var feedingTime = startTime.AddHours(4); //when you should feed it again
var span = feedingTime.Subtract(startTime);//total span between start and end
//divide the current minutes by the total minutes
//e.g 60min / 240min = 0.25 = 25%
var timeLeft = (feedingTime.Subtract(halfWay).TotalMinutes);
var difference = timeLeft / span.TotalMinutes; //gives 0.5
Can't help you with the sliders because you haven't said which UI system you're using. Most sliders have a property for the percentage though. Which I have provided for you.
Hi myzzie Thank you for your response. I'm just using the unity canvas slider. Thank you for your code. I didn't take the "halfWay" into consideration before. I'm still unsure on how to get a slider to do what i want it to do but I'll play around with it and try and get the desired result.