Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Using Exp for zooming
- One of the things I’m happiest to have learned in the past few months is a great use for Log + Exp in games when zooming in and out.
- You may have already know that linear speeds work horribly for zooming, e.g.:
- void Update() {
- scale = scale + speed * Time.deltaTime;
- }
- …slowing down horribly as you zoom out and speeds up way too fast when you zoom in.
- Instead, a better way is to do something like:
- scale = scale * (1 + speed * Time.deltaTime);
- …where speed is positive or negative depending on whether you want to zoom in or out.
- However, I found recently that the best method of all is to use a linear scale for “number of times you’ve zoomed in or out”, and then use Exp to turn it into a scale:
- // Similar to first example - it’s linear
- zoomLevels = zoomLevels + speed * Time.deltaTime;
- scale = originalScale * Mathf.Exp(zoomLevels);
- So, say your originalScale is 1.0. If your `zoomLevel` is zero, then `e^0 = 1`, so we stay at the original zoom level. When you “zoom in once”, i.e. when `zoomLevels == 1`, then the scale is `e^1 = e`. And if you zoom in 3 times, then the scale is `e^3`, or `e * e * e`, since you’ve zoomed in “by e”, three times. It works when `zoomLevels` goes negative too.
- And the best thing of all, is that if you’re internally using the linear `zoomLevels` scale, then it works great with functions like Unity’s `Mathf.SmoothDamp`, or simple `Mathf.Lerp` - it’ll zoom in out totally smoothly without having weird accelerations or decelerations at the start or end.
Add Comment
Please, Sign In to add comment