- Home /
Clamp Mouse Zoom using Scroll Wheel (SOLVED)
Hello, I'm making a RTS game and i created a little line of code to control mouse zoom here's the code.
var ROTSpeed = 10f;
function Update()
{
gameObject.transform.Translate(0,0,Input.GetAxis("Mouse ScrollWheel") * ROTSpeed);
}
So that's My code, it works just fine. Perfectly in fact, but I have one problem i can zoom in and out infinitely. Does anyone know how i could clamp the zoom? Thanks in advance! :D
Just use a float for max and $$anonymous$$ zoom values.
var ROTSpeed = 10f;
var $$anonymous$$ : float = -10.0;
var max : float = 10.0;
function Start()
{
$$anonymous$$ = Camera.main.fov + $$anonymous$$;
max = Camera.main.fov + max;
}
function Update()
{
if(Camera.main.fov <= $$anonymous$$)
Camera.main.fov = $$anonymous$$;
if(Camera.main.fov >= max)
Camera.main.fov = max;
Camera.main.fov += Input.GetAxis("$$anonymous$$ouse ScrollWheel") * ROTSpeed;
}
:/ this isn't working for me. What is Camera suppose to be. I don't see it declared anywhere... And this script is supposed to be on the main camera. I tried making a var called thisCam and then replaced all the Camera parts with thisCam and then i set thisCam to be the main camera. And I'm getting a error. it just says,
NullReferenceException UnityEngine.Camera.get_fov ()
Camera.main is the main camera. All you have to do is make sure the camera in your scene is tagged "$$anonymous$$ain Camera" in the inspector. Once you do this the script will no longer return null.
You could just add
var cam : Camera;
at the top of your script, then drag which camera you want to use. then you'd just use cam.fov.
Answer by Filippo94 · Jul 06, 2013 at 07:26 PM
Store the amount in a variable, then clamp it. For example (UnityScript):
var ZoomAmount : float = 0; //With Positive and negative values
var MaxToClamp : float = 10;
var ROTSpeed : float = 10;
function Update() {
ZoomAmount += Input.GetAxis("Mouse ScrollWheel");
ZoomAmount = Mathf.Clamp(ZoomAmount, -MaxToClamp, MaxToClamp);
var translate = Mathf.Min(Mathf.Abs(Input.GetAxis("Mouse ScrollWheel")), MaxToClamp - Mathf.Abs(ZoomAmount));
gameObject.transform.Translate(0,0,translate * ROTSpeed * Mathf.Sign(Input.GetAxis("Mouse ScrollWheel")));
}
Untested.
This produced errors for me:
Assets/$$anonymous$$WZoom2.cs(6,24): error CS1519: Unexpected symbol `:' in class, struct, or interface member declaration
Assets/$$anonymous$$WZoom2.cs(6,32): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
Assets/$$anonymous$$WZoom2.cs(7,24): error CS1519: Unexpected symbol `:' in class, struct, or interface member declaration
Assets/$$anonymous$$WZoom2.cs(7,32): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
Assets/$$anonymous$$WZoom2.cs(8,22): error CS1519: Unexpected symbol `:' in class, struct, or interface member declaration
Assets/$$anonymous$$WZoom2.cs(8,30): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
Script file:
using UnityEngine;
using System.Collections;
public class $$anonymous$$WZoom2 : $$anonymous$$onoBehaviour {
var ZoomAmount : float = 0; //With Positive and negative values
var $$anonymous$$axToClamp : float = 10;
var ROTSpeed : float = 10;
function Update() {
ZoomAmount += Input.GetAxis("$$anonymous$$ouse ScrollWheel");
ZoomAmount = $$anonymous$$athf.Clamp(ZoomAmount, -$$anonymous$$axToClamp, $$anonymous$$axToClamp);
var translate = $$anonymous$$athf.$$anonymous$$in($$anonymous$$athf.Abs(Input.GetAxis("$$anonymous$$ouse ScrollWheel")), $$anonymous$$axToClamp - $$anonymous$$athf.Abs(ZoomAmount));
gameObject.transform.Translate(0,0,translate * ROTSpeed * $$anonymous$$athf.Sign(Input.GetAxis("$$anonymous$$ouse ScrollWheel")));
}
}
Answer by newking777 · Jan 06, 2017 at 04:59 PM
For those that still need an answer to this, using Mathf Clamp is not the best way. Instead, simply write the following code on Update()
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (mycamera.fieldOfView > 1)
{
mycamera.fieldOfView--;
}
}
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (mycamera.fieldOfView < 100)
{
mycamera.fieldOfView++;
}
}
This only zooms in if the field of view is greater than the minimum (which is 1 for all cameras). The maximum value is greater than 100 but 100 is a good value.