Zooming in?

Hi, I need a script that will allow my player to go into zoom view whenever the shift key is pressed. Im thinking of changing the camera’s field of view, but i did’nt see anything about that in the scripting referance… So, how would I make a zoom view? Also, with certain weapons, (Ill worry about this later) I need it to go to zoom view at a certain place to look down the sight, eg…, sniper, mp5, ect… :smile: :smile: :smile:

I think field of view might be a good place to start when thinking about a zoom function, yes.

But you say you couldn’t find anything about field of view in the reference?

Go to the scripting reference. Unity - Scripting API:

Do a search for “camera” in the “search” box in the top left: Unity - Scripting API:

Then click on “Camera”: Unity - Scripting API: Camera

Then you might try clicking on “fieldofview”: Unity - Scripting API: Camera.fieldOfView

Alternatively, try typing “field of view” into the search box instead. Unity - Scripting API: and hit number one is “Camera.fieldofview”.

There is also a code snippet floating around which handles looking down a weapon’s sights. It might have some field of view changing, I can’t remember. Using the handy-dandy search feature should help you to track that down.

Ok! Thanks! But Im having a weird problem… :?

var zoom : int = 1;
var zoomOut : int = 60;
var zooming : boolean = false;

function Update()
{
	if (Input.GetButtonDown("Zoom")) {
	    zooming = true;
		Camera.main.fieldOfView = zoom;
	}
	
	if (!Input.GetButtonDown("Zoom")) {
		zooming = false;
		Camera.main.fieldOfView = zoomOut;
	}
}

For some reason is always trying to !zooming (lol). But why? I tried a bunch of different ways of placing the booleans, nothing is working… Can you help?

Got it! :

var zoom : int = 1;
var zoomOut : int = 60;
var zooming : boolean;

function Update()
{
	if (Input.GetButton ("Zoom")) {
	    zooming = true;
	    ZoomIn();
	}
	
	if (!Input.GetButton ("Zoom")  zooming)  {
		ZoomOut();
	}
}

function ZoomOut()
{
	Camera.main.fieldOfView = zoomOut;
}

function ZoomIn()
{
	Camera.main.fieldOfView = zoom;
}

Wow, I wrote and de-bugged it all by myself! :wink: I wonder how GetButtonDown did anything… oh, I think the bool saw that it was being changed to false right when it was pressed, not through the whole thing… if that made sense… lol

Lets start with the picky stuff.

Having an “if (x)” followed by an “if (!x)” could probably be an “if (x)” … “else” structure instead.

Also, “fieldOfView” wants a float. The documentation says that fieldOfView is a float, so unless you have a reason, feed it a float, not an int.

If your changing of values in the main camera isn’t changing your view, check that you’re actually looking through the main camera.

I’m not sure what you mean by “always trying to !zooming”.

Wasent thinking else…

I made fieldOfView a integer because I have no reason to go with a float. Integers work fine…

There’s only one camera in the scene…

I mean not zooming, eg… returning to FOV of 60…

But thanks, This is cool… :shock: :smile: 8)

You could also move your camera a bit further in the direction you are currently looking in.