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!
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.