- Home /
Sniper zooming (toggle)
how do i make it so when i click "z" once, and don't hold it, the camera will change.
private var baseFOV : float;
function Start () { baseFOV = Camera.main.fieldOfView; }
function Update () { if (Input.GetKey("z")) Camera.main.fieldOfView = 10; else Camera.main.fieldOfView = baseFOV;
}
Thats my script and if i hold z it will zoom in but how do i make it toggle on/off. and then how do i make all that so when i Zoom In there is a .5 sec delay.
Answer by Murcho · Jun 24, 2010 at 03:45 AM
Just store a bool in your script to toggle it like so
private var baseFOV : float; private var zoomToggle : bool;
function Start () { baseFOV = Camera.main.fieldOfView; zoomToggle = false; }
function Update () { if (Input.GetKey("z")) { if(zoomToggle) { zoomToggle = !zoomToggle; Camera.main.fieldOfView = 10; } else { zoomToggle = !zoomToggle; Camera.main.fieldOfView = baseFOV; } } }
You will have to be a bit more specific about you 0.5 second delay. Do you want the delay to be while zooming in, do you want it to be a delay between pressing the button then applying the new zoom FOV?
EDIT : Fixed the toggle, wasn't actually toggling.
To add the pause, you can yield, but you can't do that inside an Update loop, so will have to add another function to handle it.
function Zoom(float zoomValue) {
yield WaitForSeconds(0.5);
Camera.main.fieldOfView = zoomValue;
}
Replace the calls in the update loop that set the fov value with a call to the Zoom function and it should work.
... i don't understand the delay? how do i put it in?(I'm the worst scripter ever i only started trying to make my own scripts like a week ago... and i never followed any scripting tutorials?)
Sorry for so many questions and stuff but also it toggles in perfect but it is difficult to toggle it out you have to hold it like for 2 frames and then let go our something.
Answer by mirw9 · Jun 24, 2011 at 02:45 PM
use this
var camera1 : Camera;
var camera2 : Camera;
function Start () {
camera1.enabled = true;
camera2.enabled = false;
}
function Update () {
if (Input.GetKeyDown ("z")){
camera1.enabled = false;
camera2.enabled = true;
}
if (Input.GetKeyDown ("z")){
camera1.enabled = true;
camera2.enabled = false;
}
}
i use a modified version
EDIT: to make it so you have to press the button again to go back to normal view use this:
var camera1 : Camera;
var camera2 : Camera;
function Start () {
camera1.enabled = true;
camera2.enabled = false;
}
function Update () {
if (Input.GetKeyDown ("z")){
camera1.enabled = false;
camera2.enabled = true; }
if (Input.GetKeyUp ("z")){
camera1.enabled = true;
camera2.enabled = false; }
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613062856im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Toggle zoom 1 Answer
animations back and forth Again 0 Answers
Can I make animations snap to a frame? 1 Answer
Make ball flip around cube... 0 Answers
animation script help 0 Answers