Since I am a complete beginner to this, this script probably and most likely has its flaws. However, it does work. It might get newbies like me to get the hang of the zooming system
My script is based on bits and pieces of code I saw here and there while trying to acquire information on this very subject. However I have forgotten where all I saw these bits and pieces of information,. So, credit wherever it is due.
function Update ()
{
// -------------------Code for Zooming Out------------
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (Camera.main.fieldOfView<=125)
Camera.main.fieldOfView +=2;
if (Camera.main.orthographicSize<=20)
Camera.main.orthographicSize +=0.5;
}
// ---------------Code for Zooming In------------------------
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (Camera.main.fieldOfView>2)
Camera.main.fieldOfView -=2;
if (Camera.main.orthographicSize>=1)
Camera.main.orthographicSize -=0.5;
}
// -------Code to switch camera between Perspective and Orthographic--------
if (Input.GetKeyUp(KeyCode.B ))
{
if (Camera.main.orthographic==true)
Camera.main.orthographic=false;
else
Camera.main.orthographic=true;
}
}
dude if you’re a newbie , what am i then? XD… i tried to implement your script on my game so i could tell ya , but i just realized i dont have the mouse scroller configured on the project input setings, and i have no idea how to do it… i tried reading the docs again
but the scroller is not mentioned in there… anyway… iswitched to normal buttons from the keyboard and it kinda works!.. it just need to have a limit set so the player cant zoom soo in like it did with me
I think the Mouse ScrollWheel is automatically configured. If you look just below MouseY it is there.
Also while reading around I did see posts about problems with the Mouse scroll for users with a Mac AND a logitech mouse if I am not mistaken.
The way I got my script to work was to drag and drop it on to the main camera.
It appears there was a problem some time ago as pointed out in that thread, but it might have been fixed by now. If you read that thread, what some posters are saying looks as though the problem may not be with unity but rather the combo of mac and logitech. I am no expert on this though
And I forgot to add. Criticisms and improvements to my very crude script are welcome and in fact requested. I have a thick skin. And it will help me and other newbies learn
using UnityEngine;
using System.Collections;
public class CameraZoom : MonoBehaviour {
void Update (){
// -------------------Code for Zooming Out------------
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (Camera.main.fieldOfView<=125)
Camera.main.fieldOfView +=2;
if (Camera.main.orthographicSize<=20)
Camera.main.orthographicSize +=0.5f;
}
// ---------------Code for Zooming In------------------------
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (Camera.main.fieldOfView>2)
Camera.main.fieldOfView -=2;
if (Camera.main.orthographicSize>=1)
Camera.main.orthographicSize -=0.5f;
}
// -------Code to switch camera between Perspective and Orthographic--------
if (Input.GetKeyUp(KeyCode.B ))
{
if (Camera.main.orthographic==true)
Camera.main.orthographic=false;
else
Camera.main.orthographic=true;
}
}
}
and just to show off the boo version
import UnityEngine
import System.Collections
public class CameraZoom(MonoBehaviour):
private def Update():
// -------------------Code for Zooming Out------------
if Input.GetAxis('Mouse ScrollWheel') < 0:
if Camera.main.fieldOfView <= 125:
Camera.main.fieldOfView += 2
if Camera.main.orthographicSize <= 20:
Camera.main.orthographicSize += 0.5F
// ---------------Code for Zooming In------------------------
if Input.GetAxis('Mouse ScrollWheel') > 0:
if Camera.main.fieldOfView > 2:
Camera.main.fieldOfView -= 2
if Camera.main.orthographicSize >= 1:
Camera.main.orthographicSize -= 0.5F
// -------Code to switch camera between Perspective and Orthographic--------
if Input.GetKeyUp(KeyCode.B):
if Camera.main.orthographic == true:
Camera.main.orthographic = false
else:
Camera.main.orthographic = true
Thank you for your code.
I am also a beginner to Unity. I’ve just slightly modified your code and here is its modified version and equivalent C# version;
Java script:
function Update ()
{
// -------------------Code for Zooming Out------------
if ((Input.GetAxis("Mouse ScrollWheel") < 0) || ((Input.GetKey(KeyCode.Z)) (Input.GetKey(KeyCode.LeftShift))) )
{
if (Camera.main.fieldOfView <= 125)
Camera.main.fieldOfView += 2;
if (Camera.main.orthographicSize <= 20)
Camera.main.orthographicSize +=0.5;
}
// ---------------Code for Zooming In------------------------
if ((Input.GetAxis("Mouse ScrollWheel") > 0) || ((Input.GetKey(KeyCode.Z)) (!Input.GetKey(KeyCode.LeftShift))) )
{
if (Camera.main.fieldOfView>2)
Camera.main.fieldOfView -=2;
if (Camera.main.orthographicSize>=1)
Camera.main.orthographicSize -=0.5;
}
// -------Code to switch camera between Perspective and Orthographic--------
if (Input.GetKeyUp(KeyCode.B ))
{
if (Camera.main.orthographic==true)
Camera.main.orthographic=false;
else
Camera.main.orthographic=true;
}
}
C# code:
using UnityEngine;
using System.Collections;
public class Zoom : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// -------------------Code for Zooming Out------------
if ((Input.GetAxis("Mouse ScrollWheel") < 0) || ((Input.GetKey(KeyCode.Z)) (Input.GetKey(KeyCode.LeftShift))) )
{
if (Camera.main.fieldOfView <= 125)
Camera.main.fieldOfView += 2;
if (Camera.main.orthographicSize <= 20)
Camera.main.orthographicSize += 0.5f;
}
// ---------------Code for Zooming In------------------------
if ((Input.GetAxis("Mouse ScrollWheel") > 0) || ((Input.GetKey(KeyCode.Z)) (!Input.GetKey(KeyCode.LeftShift))) )
{
if (Camera.main.fieldOfView>2)
Camera.main.fieldOfView -=2;
if (Camera.main.orthographicSize>=1)
Camera.main.orthographicSize -= 0.5f;
}
// -------Code to switch camera between Perspective and Orthographic--------
if (Input.GetKeyUp(KeyCode.B ))
{
if (Camera.main.orthographic==true)
Camera.main.orthographic=false;
else
Camera.main.orthographic=true;
}
}
}
Heres the Boo version of yours as well, not sure if anyone uses Boo but
import UnityEngine
import System.Collections
public class Zoom(MonoBehaviour):
// Use this for initialization
private def Start():
pass
// Update is called once per frame
private def Update():
// -------------------Code for Zooming Out------------
if (Input.GetAxis('Mouse ScrollWheel') < 0) or (Input.GetKey(KeyCode.Z) and Input.GetKey(KeyCode.LeftShift)):
if Camera.main.fieldOfView <= 125:
Camera.main.fieldOfView += 2
if Camera.main.orthographicSize <= 20:
Camera.main.orthographicSize += 0.5F
// ---------------Code for Zooming In------------------------
if (Input.GetAxis('Mouse ScrollWheel') > 0) or (Input.GetKey(KeyCode.Z) and (not Input.GetKey(KeyCode.LeftShift))):
if Camera.main.fieldOfView > 2:
Camera.main.fieldOfView -= 2
if Camera.main.orthographicSize >= 1:
Camera.main.orthographicSize -= 0.5F
// -------Code to switch camera between Perspective and Orthographic--------
if Input.GetKeyUp(KeyCode.B):
if Camera.main.orthographic == true:
Camera.main.orthographic = false
else:
Camera.main.orthographic = true