- Home /
Mathf.SmoothDamp happens instantly
So i have 2 functions for zooming the gun out and one for zooming the gun out.
The zoom out gets called in Update() when the player speed reaches a certain number as well as the zoom in.
Here are the functions:
void MakeGunFurther()
{
float refVel = 0;
float normalFOV = gunCam.fieldOfView;
gunCam.fieldOfView = Mathf.SmoothDamp(normalFOV, 55f, ref refTest, 0.1f);
}
void MakeGunCloser()
{
float refVel = 0;
gunCam.fieldOfView = Mathf.SmoothDamp(47, 55, ref refTest, 0.1f);
hasRan = false;
}
Now, my problem is that the first function works fine and makes the gun smoothly zoom out, BUT when i need to zoom it in again with the second function it just instantly does it not smoothly like the first one.
Please help. Thanks!
Answer by Bunny83 · Oct 20, 2020 at 04:05 PM
You do know what the reference velocity is good for, right? It's there to represent the velocity over time. So the method is changing the velocity and is expecting to get that same value the next time it's called. It's a state variable. You declared it locally and just set it to 0. Next thing is your last parameter is the smooth time which you set to 0.1 seconds. So you expect the smoothing to finish after a tenth of a second (or 100ms) which to a human would probably look almost like an instant change. You should do this:
float dampingVelocity = 0f;
void MakeGunFurther()
{
gunCam.fieldOfView = Mathf.SmoothDamp(gunCam.fieldOfView, 55f, ref dampingVelocity, 0.5f);
}
void MakeGunCloser()
{
gunCam.fieldOfView = Mathf.SmoothDamp(gunCam.fieldOfView, 47, ref dampingVelocity, 0.5f);
}
Please keep in mind that you have to execute one of the two methods every frame in order to work. Usually you just do something like this:
float dampingVelocity = 0f;
float targetFOV = 55f;
void Update()
{
gunCam.fieldOfView = Mathf.SmoothDamp(gunCam.fieldOfView, targetFOV, ref dampingVelocity, 0.5f);
}
void SetNormalFov()
{
targetFOV = 55f;
}
void SetZoomFov()
{
targetFOV = 47f;
}
In this case you can call either SetNormalFov or SetZoomFov once and the Smoothdamp function will take care of bringing you to that new target FOV within half a second.
Thank you soo much it works!! you just have a tiny error in you code because you forgot to assign targetFOV to 47f in Start() So i did that and it worked!!
Yep, totally my fault that I did not predict that right. Should have been obvious from your code that 47 is the default and not 55 -.-
btw I would consider fovs around 55 or 47 as a high zoom fov. I usually play most games with a fov between 110 and 120 ^^ I just checked my old quake config and yes, 120 default and 40 is my high zoom fov (which is even too much for most smaller maps).
Your answer
Follow this Question
Related Questions
If game object is in camera's field of view 3 Answers
Why does it bug when i want to rotate my camara 0 Answers
field of view slider starting point 1 Answer
Camera Dampening 1 Answer
Lerping Field Of View is buggy 1 Answer