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 /
  • Help Room /
avatar image
0
Question by KevinKLV · Feb 02, 2017 at 08:12 AM · cameralerpmathf.lerpfield of viewzoom-in

[Solved ]Zoom Main Camera Smoothly

Objective

I want to zoom the main camera by changing the field of view, and I want it to happen smoothly.

Problem

I am getting the zoom in, but it is too fast. I have change variables and played with number but still is either too fast or it does not happen.

Code


 public class ZoomInOut : MonoBehaviour {
 
     //public variables parameter for Unity inspector input
     public float initialFOV;
     public float zoomInFOV;
     public float smooth;
 
     //private variables for script mod
     private float sphereRotation;
     private float lerpTimer;
 
     // Use this for initialization
     void Start ()
     {
         Camera.main.fieldOfView = initialFOV;
     }
     
     // Update is called once per frame
     void Update ()
     {
             ChangeFOV();
     }
 
     void ChangeFOV()
     {
         lerpTimer = Time.deltaTime * smooth;
         Camera.main.fieldOfView = Mathf.Lerp(initialFOV, zoomInFOV, lerpTimer);
     }
 }



Thanks in advance!

Comment
Add comment · Show 2
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 Owen-Reynolds · Feb 02, 2017 at 03:00 PM 0
Share

This is really about learning basic program$$anonymous$$g and "could someone check my code." We do allow that now, but could you post it to the Help Room area? Thanks.

You're also using a do-nothing Lerp. You should be able to find a discussion here that starts with your line 27 and ask "why isn't this working?" where people have explained it.

avatar image KevinKLV Owen-Reynolds · Feb 02, 2017 at 09:11 PM 0
Share

Thanks @Owen-Reynolds will post in help room next time. I don't know what do you mean with the do-nothing Lerp. Will research on that. I'm still getting my head around how that method work. Sometimes I get it right other I don't. I know it is a concept problem, but I haven't had the time to read fully about it. Particularly on how to use the Time.time and Time.deltaTime. C# Unity noob here.

3 Replies

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

Answer by KevinKLV · Feb 02, 2017 at 04:03 AM

In case you would like to know how I solved it.

Objective

  1. Check when rotation stopped

  2. Change the FOV to the Zoom-In parameter

  3. Make the change in an incremental smooth manner

Solution

Instead of using mathf.lerp I changed to if statements. I started checking what was the current FOV after rotation stopped. When it started to Zoom-In I checked the FOV until it was the same or smaller than my Zoom-In value, at which point I set it equal to my Zoom-In input. I change the value of the FOV using an incremental.

Code


 public class ZoomInOut : MonoBehaviour
 {
 
     //public variables paremeters for Unity inpector inputs
     public float initialFOV;
     public float zoomInFOV;
     public float smooth;
 
     //private variables for script mod
     private float sphereRotation;
     private float currentFOV;
 
     // Use this for initialization
     void Start()
     {
         //set initial FOV at start
         Camera.main.fieldOfView = initialFOV;
     }
 
     // Update is called once per frame
     void Update()
     {
         //get component from sphere script to know if rations is happening
         sphereRotation = GameObject.Find("Sphere").GetComponent<Rotation>().rotationSpeed;
         
          //store current field of view value in variable
         currentFOV = Camera.main.fieldOfView;
 
         //check if roration stopped and call function to change FOV
         if (sphereRotation <= 0)
         {
             ChangeFOV();
         }
     }
 
     //function to zoom in the FOV
     void ChangeFOV()
     {
         //check that current FOV is different than Zoomed
         if (currentFOV != zoomInFOV)
         {
             //check if current FOV is grater than the Zoomed in FOV input and increment the FOV smoothly
             if (currentFOV > zoomInFOV)
             {
                 Camera.main.fieldOfView += (-smooth * Time.deltaTime);
             }
             else
             {
                 //then current FOV gets to the same or smaller value than the Zoomed in input
                 //set FOV as the Zoomed in input
                 if (currentFOV <= zoomInFOV)
                 {
                     Camera.main.fieldOfView = zoomInFOV;
                 }
             }
         }
     }
 }



This was my solution to my problem. Please feel free to criticize and comment on it. I tried the math.lerp but I was not able to get the smooth transition I wanted. If you have a better way, you can post a working example.

Cheers

Comment
Add comment · 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
1

Answer by ASPePeX · Feb 02, 2017 at 12:43 PM

Here is a solution using Lerp with a few examples how to smooth it out. Just slap this script on a camera and it should work.

All that matters is in the 4 lines within the first if statement in the ChangeFOV() method. The magic is how you calculate t, the actual interpolation. I have 4 examples there: Smoothstep and SmootherStep smoothing in and out, quadratic and cubic just easing in (could be inverted to ease out).

 using System;
 using UnityEngine;
 
 [RequireComponent(typeof(Camera))]
 public class FoviLerp : MonoBehaviour {
     public float FovStart = 60;
     public float FovEnd = 30;
     public float TransitionTime = 3;
 
     private float _currentFov;
     private float _lerpTime;
     private Camera _camera;
 
     void Start ()
     {
         //could also use Camera.main if apropriate
         _camera = this.GetComponent<Camera>();
     }
     
     void Update () {
         ChangeFOV();
     }
 
     void ChangeFOV()
     {
         if (Math.Abs(_currentFov - FovEnd) > float.Epsilon)
         {
             _lerpTime += Time.deltaTime;
             var t = _lerpTime / TransitionTime;
 
             //Different ways of interpolation if you comment them all it is just a linear lerp
             t = Mathf.SmoothStep(0, 1, t); //Mathf.SmoothStep() can be used just like Lerp, here it is used to calc t so it works with the other examples.
             //t = SmootherStep(t);
             //t = t * t;
             //t = t * t * t;
 
             _currentFov = Mathf.Lerp(FovStart, FovEnd, t);
         }
         else if (Math.Abs(_currentFov - FovEnd) < float.Epsilon)
         {
             //Just going back where we came from. For demonstrative purpos only ...
             _lerpTime = 0;
             Debug.Log("Switch");
             var tmp = FovStart;
             FovStart = FovEnd;
             FovEnd = tmp;
         }
 
         _camera.fieldOfView = _currentFov;
     }
 
     private float SmootherStep(float t)
     {
         return t * t * t * (t * (6f * t - 15f) + 10f);
     }
 }
Comment
Add comment · Show 1 · 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 KevinKLV · Feb 02, 2017 at 08:56 PM 0
Share

This looks great! I'm gonna give it a go! Thanks @ASPePex

avatar image
0

Answer by jaredKreate · Jul 04, 2017 at 01:30 AM

Hey, you may find these videos helpful!

https://youtu.be/IrSh0GLo3As

https://youtu.be/0cWvJEokGsw

We complete a lot of scripts like this one on our channel, and we just happened to complete these fairly recently. Hope this helps!

Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

How to move a camera smoothly to a fixed position and back using C#? 0 Answers

Lerp Not working 1 Answer

Camera view distorted in build, but fine in game view within the Editor. 0 Answers

Is there a way to smoothly change a float number between two numbers when a key is pressed? 1 Answer

Zoom or magnify glass effect without camera. Help!! 0 Answers


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