Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 alexanderameye · Feb 09, 2014 at 05:54 PM · c#zoomfadecamera-scrolling

How to make a smooth zoom camera script with maximums?

I implemented this piece of code in my camera script, and it works fine, but how can I make a min - max zoom setting, and how can I make it that when I zoom in, it zooms in just a little bit further, like a zoom fade in and fade out. It needs to be smooth. Thank you! using UnityEngine; using System.Collections;

 public class Zoom : MonoBehaviour {
     
     public float zoomSpeed = 20;
     
     void Update () 
     {
         
         float scroll = Input.GetAxis("Mouse ScrollWheel");
         if (scroll != 0.0f)
         {
             camera.fieldOfView -= scroll*zoomSpeed;
         }
     }
 }
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 highpockets · Feb 09, 2014 at 06:03 PM 0
Share

Take a look at the smoothdamp function: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.SmoothDamp.html

avatar image highpockets · Feb 09, 2014 at 06:16 PM 0
Share

Oops, I didn't see that you were using field of view. The Lerp function will give you a nice interpolation between the start value and end value over a timescale

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · Feb 09, 2014 at 06:22 PM

Do something like this:

 float currentFOV = camera.fieldOfView
 
 camera.fieldOfView = Mathf.Lerp(currentFOV, scroll, Time.time * zoomSpeed);

I didn't test this out, but it should do the trick

Comment
Add comment · Show 6 · 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 alexanderameye · Feb 09, 2014 at 06:55 PM 0
Share

hm, it doesnt work :(

avatar image alexanderameye · Feb 09, 2014 at 07:05 PM 0
Share

I tested this, and it works, but it zooms extremely slow suddenly? using UnityEngine; using System.Collections;

 public class TPCamera : $$anonymous$$onoBehaviour
 {
     public float Smooth = 3f;
     public float ZoomSpeed = 4f;
     
     Transform standardPos;            
     Transform lookAtPos;            
 
     void Start()
     {
         
 
         standardPos = GameObject.Find ("CamPos").transform;
         
         if(GameObject.Find ("LookAtPos"))
             lookAtPos = GameObject.Find ("LookAtPos").transform;
                           
 
     }
     
     void Update ()
     {
     
 
         transform.position = Vector3.Lerp(transform.position, standardPos.position, Time.deltaTime * Smooth);    
         transform.forward = Vector3.Lerp(transform.forward, standardPos.forward, Time.deltaTime * Smooth);
 
         float currentFOV = camera.fieldOfView;
         float scroll = Input.GetAxis("$$anonymous$$ouse ScrollWheel");
         if (scroll != 0.0f)
         {
             camera.fieldOfView -= $$anonymous$$athf.Lerp(currentFOV, scroll, Time.time * ZoomSpeed);
 
         }
 
 
             
avatar image highpockets · Feb 10, 2014 at 06:09 AM 0
Share

Oh, you were already using lerp functions for the position, I was wondering why you didn't have a transform movement in there..

I failed you the first time, but I put in a better effort here: (comments in code)

 public class TPCamera : $$anonymous$$onoBehaviour
 {
     public float smooth = 3f;
     
     //Set $$anonymous$$ and max FOV and declare the difference variable
     public float $$anonymous$$FOV = 20;
     public float maxFOV = 60;
     private float diffFOV;
     
     //declare a time scale and a rate at which the object should move from position to target
     private float timeScale = 0.0f;
     public float zoomSpeed = 4f;
      
     //declare a variable to store the last target FOV to test against the current target FOV
     private float lastTargetFOV;
     
     Transform standardPos;        
     Transform lookAtPos;      
  
     void Start()
     {
        
         standardPos = GameObject.Find ("CamPos").transform;
         
     
        if(GameObject.Find ("LookAtPos"))
          lookAtPos = GameObject.Find ("LookAtPos").transform;
         
         //Find the difference between your $$anonymous$$ and max FOV
         diffFOV = maxFOV - $$anonymous$$FOV;
     }
     
     void Update ()
     {
     
     
         transform.position = Vector3.Lerp(transform.position, standardPos.position, Time.deltaTime * smooth); 
         transform.forward = Vector3.Lerp(transform.forward, standardPos.forward, Time.deltaTime * smooth);
         
         float startFOV;
         
         //$$anonymous$$ultiply the axis by the difference of $$anonymous$$ and max FOV
         float targetFOV = Input.GetAxis("$$anonymous$$ouse ScrollWheel");
         
         if( targetFOV > 0 )
         {
             
             //Add $$anonymous$$ FOV to find target FOV
             targetFOV += $$anonymous$$FOV;
             
             //$$anonymous$$ake sure target does not exceed max
             if( targetFOV > maxFOV )
             {
                 
                 targetFOV = maxFOV;
             }
         }
         
         if( targetFOV < 0 )
         {
             
             //Add max to find target FOV
             targetFOV += maxFOV;
             
             //$$anonymous$$ake sure target is not lower than $$anonymous$$
             if( targetFOV < $$anonymous$$FOV )
             {
                 
                 targetFOV = $$anonymous$$FOV;
             }
         }
         
         //check to see if the camera FOV made it to the target
         if ( camera.fieldOfView != targetFOV )
         {
             
             //If there has been no change to target via scroll or max/$$anonymous$$ are reached, than calculate the time scale for the lerp
             if( taretFOV == lastTargetFOV )
             {
                 
                 timeScale += Time.deltaTime * zoomSpeed;
             }
             
             //reset time scale and start point if the target has changed
             else
             {
                 
                 timeScale = 0.0f;
                 startFOV = camera.fieldOfView;
             }
             
             //Lerp from start to target FOV over time scale
             camera.fieldOfView = $$anonymous$$athf.Lerp( startFOV, targetFOV, timeScale );
         }
         
         //save the last target FOV to test if target has changed next frame
         lastTargetFOV = targetFOV;
     }
 }

Please tell me it works.. I did not test it though... I don't know what kind of values are given out with mouse scroll wheel, so you may need to slow that number down or speed it up.. I guess it depends on mouse sensitivity.. Play with the zoomSpeed even around 0.5f to get a feel for whats going on there.

Good luck

avatar image highpockets · Feb 10, 2014 at 08:38 AM 0
Share

Hey, I also want to add that, unless you are doing this scrolling all the time, you should put the process in a coroutine and listen for the event of the mouse scrolling to activate the coroutine because update will check if this is happening once per frame, which is unnecessary unless you are doing it constantly

avatar image alexanderameye · Feb 12, 2014 at 05:09 PM 0
Share

Hey, there are a few bugs in the script: 1) Somewhere "target" is missspelled 2) You get an error for the float startFOV, you can fix this by moving it before the start function 3) When running, it works a little, but i get 999+ errors, and it always returns to a certain zoom point. I extremely appreciate your help, really! But I just actually found another good zoom script that I then adjusted a little, if you are interested to take a look at it, just tell me and I well post it here. Thanks again!

Show more comments

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

20 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

Related Questions

Problem with getkey/getkeydown 1 Answer

Different views for graph plotting 0 Answers

Script to constantly change FOV? 1 Answer

go back to normal? 1 Answer

Fading sprite issue 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