I have some code here that works as intended, however I couldn’t find a way to condense this code to one line. The gain in performance will probably be negligible but I’m curious if someone has a better solution.
The objective is to decrease the fatigue while remaining clamped within two values.
Thanks for you help.
void FixedUpdate()
{
fatigue -= (0.1f * Time.deltaTime);
fatigue = Mathf.Clamp(fatigue, 0.0f, 100.0f);
}
one liner:
fatigue = Mathf.Clamp(fatigue - (0.1f*Time.fixedDeltaTime), 0.0f, 100.0f);
typically you want to use fixedDeltaTime instead of deltaTime inside FixedUpdate.
1 Like
It’s simple to put it in one line. Just put the first calculation into the clamp.
But don’t confuse the number of lines with any kind of performance.
You have no idea what clamp is doing. It could have 1 operation or 1000.
Lines are not a good metric of performance. If you tried to count operations would be better.
Clamp is going to have some ifs in it most likely.
Depending on the operation it might be better to do the ifs first then the operation.
If the operation took 1 second every time. Why do it every frame if you are just going to throw away the result.
Check to see if the output could be in range first then do it. Otherwise just set it to your min or max range.
I would say your case is to simple to worry about.
1 Like
Thanks very much guys great help.