Guest User

Untitled

a guest
Jan 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. # Using Exp for zooming
  2.  
  3. 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.
  4.  
  5. You may have already know that linear speeds work horribly for zooming, e.g.:
  6.  
  7. void Update() {
  8. scale = scale + speed * Time.deltaTime;
  9. }
  10.  
  11. …slowing down horribly as you zoom out and speeds up way too fast when you zoom in.
  12.  
  13. Instead, a better way is to do something like:
  14.  
  15. scale = scale * (1 + speed * Time.deltaTime);
  16.  
  17. …where speed is positive or negative depending on whether you want to zoom in or out.
  18.  
  19. 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:
  20.  
  21. // Similar to first example - it’s linear
  22. zoomLevels = zoomLevels + speed * Time.deltaTime;
  23.  
  24. scale = originalScale * Mathf.Exp(zoomLevels);
  25.  
  26. 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.
  27.  
  28. 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