- Home /
Zooming in with a gun, Halo style.
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
Answer by Helix · Dec 14, 2010 at 02:53 AM
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
Thanks very much! One more thing, the zoom in is pretty slow. Is there a way to make it zoom in quickly?
Sure! I edited the code. The ++ and -- always add and subtract by 1, they gave you a fixed "zoom speed" of 1. Now you can set the zoomSpeed to whatever you like. So long as I haven't made any typos, that should work fine.
You are missing Time.deltaTime so it is frame-rate dependent which probably isn't what the OP wanted.
Thanks you both! I ended up combining the two scripts and have exactly what I was looking for.
Answer by Statement · Dec 15, 2010 at 02:35 AM
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); }
Your answer
Follow this Question
Related Questions
FPS Aim Down Sights. Exact Position. 1 Answer
animating multiple weapons with 2 arms 0 Answers
Animations have been cleared, help! 0 Answers
FPS gun movement 1 Answer
Camera changing with animation. 0 Answers