Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by big_stoater · Apr 22, 2018 at 07:17 AM · camera.mainmathf.lerpfield of view

Camera field of view and Mathf.Lerp, camera FOV not moving to value specified

Hello!

I'm using mathf.Lerp to change the camera FOV smoothly when the player hits a trigger. The script I have written for this does work, however the FOV is not changing to the value specified in the script and never goes below 30f.

I have the default FOV set to 40 and the target FOV set to 15. When the script is triggered the FOV changes from 40 to 30. I tried lowering the target FOV to 5 and but the lowest it will go when the script is ran is 27.

Am I using mathf.lerp incorrectly or should I be using something else?

Here's a snippet from my script:

 private float targetFOV = 15.0f;
  private float defaultFOV = 40.0f;
  public float speed = 0.2f;
 
  if (legsTrigger.isTriggerOn == true)
         {
             Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, targetFOV, speed * Time.deltaTime);
         }
         else if (!legsTrigger.isTriggerOn)
         {
             Camera.main.fieldOfView = Mathf.Lerp(Camera.main.fieldOfView, defaultFOV, speed * Time.deltaTime);
         }



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
0

Answer by Firas4d · Apr 22, 2018 at 08:00 AM

Hi, It seems like that the time while isTriggerOn is true isn't enough for the lerp function to reach the second parameter value, try to increase the speed to make sure this is really the case. Another note, the output of your lerps will never reach the target as long as the speed x Time.deltaTime is less than one, it will surly get closer and closer to the target each frame but it won't be exactly at the targetFOV which is 15, for instance. And if the speed x Time.deltaTime >= 1 then there will be no in-between values. Personally I'd like to keep the lerp linear to prevent any unexpected behaviors as follow :

t += Time.deltaTime * speed;

t = Mathf.Clamp(t, 0.0f, 1.0f);

Camera.main.fieldOfView = Mathf.Lerp(defaultFOV, targetFOV, t);

so at t = 0 the output is exactly equal to defaultFOV and at t = 1 the output is exactly equal to targetFOV.

Comment
Add comment · Show 3 · 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 big_stoater · Apr 22, 2018 at 08:27 AM 0
Share

Thanks for the reply, I'll give that go.

$$anonymous$$uch appreciated!

avatar image big_stoater · Apr 22, 2018 at 09:07 AM 0
Share

O$$anonymous$$, I'm a bit dense here and still learning this stuff (it's my first project).

I tried as you suggested, it works the first time the player enters the trigger, but when the player leaves the trigger or re-enters after that the camera jumps between the two values instantly with no smooth movement.

what did I do wrong?

avatar image big_stoater · Apr 22, 2018 at 09:55 AM 0
Share

So I can see what's going wrong (I think). I printed out "t" to the console. When the player enters the trigger I see "t" go from 0 to 1 and the field of view zooms in. "t" then stays at 1 whether a trigger is entered or not so I guess that's the issue? How do I fix it?

cheers

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

78 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

Related Questions

Can't use camera.main [SOLVED] 1 Answer

fps camera facing 90 degrees the wrong way, C# coding issue. 1 Answer

Camera blocked while changing scene. 0 Answers

How would you start another lerp based off of a condition of a current lerp? 1 Answer

ease in and out between blendshapes 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