Hello,
I am trying to make a game where you can press and hold a button and you will be zoomed in like in halo. So far, I have this:
function Update () {
if (Input.GetButtonDown("Fire2"))
{
camera.fieldOfView = 26;
}
else if (Input.GetButtonUp("Fire2"))
{
camera.fieldOfView = 66;
}
}
With this code, the camera just jumps forward. I want it to smoothly zoom in and out. Let me know if I can make myself clearer.
Thanks
Tate
Helix
2
Ahoy there, Tate.
Here's a quick and dirty solution. Put this in at the beginning of your script:
private var zoom : boolean = false;
and change your current code to:
var zoomSpeed : float = 2.5;
if (Input.GetButtonDown("Fire2"))
{
if(zoom==false){zoom=true;}
else{zoom=false;}
}
if(zoom==false)
{
if(camera.fieldOfView<66)
{
camera.fieldOfView = Mathf.Clamp(camera.fieldOfView+zoomSpeed,26,66);
}
}
else
{
if(camera.fieldOfView>26)
{
camera.fieldOfView = Mathf.Clamp(camera.fieldOfView-zoomSpeed,26,66);
}
}
Hope that's what you're after
Here's a configurable scope script.
You can adjust the following variables directly on the inspector, but it should work out of the box for you. It requires a camera.
- Scope Button
- Scope Speed
- Scope Fov
- Normal Fov
var scopeButton : String = "Fire2";
var scopeSpeed : float = 250;
var scopedFov : float = 26;
var normalFov : float = 66;
camera.fieldOfView = normalFov;
function Update()
{
var isScoping = Input.GetButton(scopeButton);
var targetFov = isScoping ? scopedFov : normalFov;
var speed = scopeSpeed * Time.deltaTime;
camera.fieldOfView = Mathf.MoveTowards(camera.fieldOfView, targetFov, speed);
}