- Home /
Scripting: If velocity goes up, do this...
First of all, hi there!
I'm trying to change my camera's orthographicSize as its target speeds up and slows down. Right now my "if" statement checks to see if the forward velocity of the target is above 8, and if it is, it slowly raises the camera's orthographicSize with a Mathf.Lerp operation, and when the forward velocity goes below 8, it decreases it.
This is not really giving me the effect I want though. Instead, I would like to check if the target's velocity is going up or down. I think I need to check the current forward velocity against last frame's forward velocity, that way I will know if the target's velocity is increasing or decreasing after each frame.
In the end, here's what I'm after:
"If target velocity is increasing, then start zooming out, else if target velocity is decreasing, start zooming in."
This way the faster my target goes, the more the camera will zoom out, and the slower it goes, the more it zooms in. I'm not quite sure how to check if the velocity is increasing or decreasing relative to the last frame's numbers.
Any advice would be really helpful and appreciated! Thanks :)
Answer by Eric5h5 · Aug 27, 2010 at 06:47 PM
Maybe you just want to map orthographic size based on velocity?
function Update () {
camera.orthographicSize = Mathf.Lerp (minZoom, maxZoom, Mathf.InverseLerp (0.0, maxVelocity, rigidbody.velocity.magnitude));
}
Eric, thanks for your help. I tried your code and it's almost doing what I want, I just need to "slow down" the zoo$$anonymous$$g effect now, as it zooms in and out too fast. So how can I slow down the Lerp?
Thanks
@ronronmx: you could use that code to set a target variable ins$$anonymous$$d, and then lerp between the current size and the target over time, in a similar way to the standard SmoothFollow script.
@Eric5h5: I see what you mean, I will study the SmoothFollow script and set it up like you suggested. Thanks again for the help!
Your answer
![](https://koobas.hobune.stream/wayback/20220613072213im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
needed velocity to reach target in required duration 2 Answers
Accurate and timely velocity-based movement 0 Answers
2D one distance at a time movement 2 Answers
Player slows down when jumping/velocity changes 1 Answer
Character won't move (Fixed) 1 Answer