0 to 1, in Z seconds

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.

If you know the total amount of time you want the activity to take (which you set, measured in seconds) and the amount of actual time that has elapsed (which you can compute from summing Time.deltatTime each frame), then getting a value between 0 and 1 is a simple ratio. No need for Lerp, MoveTowards, or any other function:

float totalZ = 25.0f; // say it takes 25 seconds to complete the activity
float currentZ = 0;   // the amount of time that has elapsed so far

void Update() {

  currentZ += Time.deltaTime; // add time each frame

  float percentComplete = currentZ / totalZ; // value between 0 - 1

  percentComplete = Mathf.Clamp01(percentComplete); // this prevents it exceeding 1

}

float interactionTimeStart; // when the interaction started
float interactionDuration = 5.0f; // 5 seconds

float GetInteractionProportion()
{
float proportion = (Time.timeSinceLevelLoad - interactionTimeStart) / interactionDuration;
return Mathf.Clamp01(proportion);
}

then set interactionTimeStart to Time.timeSinceLevelLoad when the player begins interacting with the object. This number from 0->1 can be used for displaying the interaction bar or multiplied by the interaction time to determine for how long the player has been interacting with the object.

Oh, do what I did with a problem I had. Create a percentage and modify to set points with that percentage.

An example if you want to move in 10 seconds (PSEUDO):

float percentage = 0;
float maxTime = 10;
float currentTime = 0;

float targetX = 50;
float startX = 10;

void Update() {
   //Calculate current percentage from time
   currentTime += Time.deltaTime;
   percentage = Mathf.clamp(currentTime / maxTime, 0F, 1F);

   //Calaculate the difference between max and min, this will be used to "step" the position
   float xDiff = targetX - startX;

   //Get the current position
   Vector3 pos = transform.position;

   //"Step" it by the current percentage
   pos.x = (xDiff * percentage) + startX;
  
   //apply the new position
   transform.position = pos;
}