I want to create function that i can trigger whenever player loses. So i want to do a little slow-mo effect. Nothing seems to work, im probably doing something wrong but i tried mathf.lerp and other stuff but i cant seem to find out any way to achieve this. Anyone have any idea ?
Mathf.MoveTowards is probably what you’re looking for.
Whenever the player dies, move the Time.timeScale
value towards 0 between frames.
Mathf.Lerp works, as will Mathf.MoveTowards or Mathf.SmoothDamp.
One caveat is that instead of using Time.deltaTime you will want to use Time.unscaledDeltaTime
otherwise the very time scale you are changing will affect the code which is changing it.
You can see example code for all of these at the following links:
Mathf.Lerp is what you want to use, but you are creating a problem;
You’d think that you could just do;
elapsed += Time.deltaTime;
Time.timeScale = Mathf.Lerp(1f, 0f, elapsed);
but the problem is that you are reducing time and the Time.deltaTime is affected by it. fixedDeltaTime (which is used by physics and should probably be reduced as well) and realtimeSinceStartup is not affectred by the timeScale
You could use those, but I’m not sure where you would run this since everything will slow down. FixedUpdate does not slow down, but it does stop completely when the TimeScale reaches 0.
This might work, but I don’t know. Wrote it in the little code tag editor thing
float elapsed = 0f;
float lastTime = 0f;
private void FixedUpdate()
{
if (lastTime == 0)
{
lastTime = Time.realtimeSinceStartup;
}
else
{
elapsed += Time.realtimeSinceStartup - lastTime;
lastTime = Time.realtimeSinceStartup;
Time.timeScale = Mathf.Lerp(1f, 0f, elapsed);
}
}
have a look at the documentation; Unity - Scripting API: Time.timeScale
Edit Do what the other guys said. Their ideas are much better. Especially the Time.unscaledDeltaTime one…
Okay guys ill give it a look in a sec and let you know, thanks for all the replys
This is backwards. Update doesn’t get run less frequently as time scale is reduced. FixedUpdate does get run less frequently when timescale is reduced.
The only thing that happens to Update is Time.deltaTime gets scaled down. Running the code in Update and using Time.unscaledDeltaTime is the way to go.
Guys you are legends, its working perfectly, thanks a lot!!!
@FogCZ
By using Lerp you also allow yourself to fine tune the actual curve and make it more smooth or “eased” as you see fit. Try looking up for “easing functions” or “tweening functions” as they’re called for a bit more control over the shape of the curve.
Or you could just as well use AnimationCurve that comes with Unity and is usable from the inspector.
See the examples on AnimationCurve to get the idea of how to use this from the code, but for such “animationy” effects you always want your key values to be in the 0…1 range, so that you can endlessly transform it and make it appear less uncanny than a typical flat ramp you normally get from “linearly interpolating” (lerp).
Uncanniness is especially noticeable when doing things like fade in/out, but I can imagine that fiddling with time can also be problematic, because our brains perceive certain things non-linearly, because they’re normally exponential (or logarithmic) in the nature (like quantity of light, for example, or audio dynamics).
Okay i didnt know that thanks, sometimes i feel like you guys are some kind of physicists D
Would it be possible to use Mathf.SmoothDamp with unscaledDeltaTime?
Let us know what you find out!
This is the general-case solution:
Smoothing movement between any two particular values:
https://forum.unity.com/threads/beginner-need-help-with-smoothdamp.988959/#post-6430100
You have currentQuantity and desiredQuantity.
- only set desiredQuantity
- the code always moves currentQuantity towards desiredQuantity
- read currentQuantity for the smoothed value
Works for floats, Vectors, Colors, Quaternions, anything continuous or lerp-able.
The code: https://gist.github.com/kurtdekker/fb3c33ec6911a1d9bfcb23e9f62adac4