- Home /
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;
}
}
}
Take a look at the smoothdamp function: http://docs.unity3d.com/Documentation/ScriptReference/Vector3.SmoothDamp.html
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
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
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);
}
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
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
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!
Your answer
Follow this Question
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