Greetings,
I’m going to try to make this question as simple as possible, as I am looking for the simplest way to achieve it. I’ll first try to explain the code I need and then try to elaborate on the scenario that I need it for to make it more understandable.
I need 0 to go from 0 to 1, in Z seconds, which can be done many different ways but on top of this I need to be able to get the % of the way 0 is to 1 at any given time. So essentially I don’t want 0 to be automatically set to 1 after Z seconds, but I need it to slowly add up towards that point over the Z time.
I assume since this is a bit hard to explain it’s probably going to be even harder to understand it, therefor I’ll try to explain the scenario of which this is needed in my game to hopefully make it clearer or simply open it for other ways to achieve the same effect:
I want the player to be able to harvest objects in the game, but to do so they need to interact with it for Z seconds for it to be completed (Z(time) is of course different from object to object, depending on how long it will take to harvest it). I’d love if it only was that simple, but I also need the % of the progression towards harvesting it, for visually presentation purposes(which isn’t important to elaborate).
I guess it’s worth mentioning that the player can cancel the interaction at any given time, and therefor the player need to start over again next time it starts to interact with the object.
I’ve made it half work through different tests, but I need the function to be made for global use for every object. So it doesn’t matter if it will take 1 second or 10 seconds to harvest it as the % will always give an appropriate amount of values before 100%(that is including 0% and 100%).
See, my problem has been that if I add lets say 0.1 every 10/Z, it will work but I’ll only get the % calculation 10 times throughout the course of the Z(time), as the game is essentially moving 0 towards 1 through only 10 steps. I get that I could then just divide the Z(time) by even more but I feel like that’s not the correct way of doing it.
I’m running this in FixedUpdate, so I’d like it if it could add to 0 towards 1 at every FixedUpdate step,
…or maybe there is a totally different way to do this that is much better so I’ll end it here and leave it to you guys to hopefully help me out.
I’m sorry for the long post,
Best Regards,
Winsj
EDIT:
I’ve tried Mathf.MoveTowards, but allthough it seem to work for now I’m not sure this is the right way to go? I’d love an opinion on the matter and please feel free to suggest other ways to achieve something like it.