• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by karim-123 · Oct 20, 2020 at 03:00 PM · cameramathfsmoothdampfieldofview

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image karim-123 · Oct 20, 2020 at 04:39 PM 0
Share

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

avatar image Bunny83 karim-123 · Oct 20, 2020 at 09:04 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

211 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Understanding smoothTime in Mathf.SmoothDamp 0 Answers

Smooth camera shift, Lerp? SmoothShift? 2 Answers

Mouse wheel zoom 1 Answer

Local Multiplayer Camera 0 Answers

How to cut a camera view in half 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges