- Home /
The question is answered, right answer was accepted
zooming in and out with mouse wheel
im making an rpg, and im now onto the stage of doing the camera. orbiting my character ect. but i dont know how to make my camera zoom in and out using the mouse wheel, can anyone help me?
Answer by _Petroz · Dec 17, 2010 at 10:01 PM
Generally 'zooming' in game is just moving the camera closer or farther away from the target. To make it zoom just adjust the camera distance with the scroll wheel.
float cameraDistanceMax = 20f;
float cameraDistanceMin = 5f;
float cameraDistance = 10f;
float scrollSpeed = 0.5f;
void Update()
{
cameraDistance += Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
cameraDistance = Mathf.Clamp(cameraDistance, cameraDistanceMin, cameraDistanceMax);
// set camera position
}
This script produced no errors, but did also not produce any results:
using UnityEngine;
using System.Collections;
public class $$anonymous$$WZoom : $$anonymous$$onoBehaviour {
float cameraDistance$$anonymous$$ax = 20f;
float cameraDistance$$anonymous$$in = 5f;
float cameraDistance = 10f;
float scrollSpeed = 0.5f;
void Update()
{
cameraDistance += Input.GetAxis("$$anonymous$$ouse ScrollWheel") * scrollSpeed;
cameraDistance = $$anonymous$$athf.Clamp(cameraDistance, cameraDistance$$anonymous$$in, cameraDistance$$anonymous$$ax);
// set camera position
}
}
Thanks man. It works just like I need it. If someone have problems with this, import the built in utility package and use script "SmoothFollow.cs", add there this lines of code:
float height$$anonymous$$ax = 10f;
float height$$anonymous$$in = 3f;
float scrollSpeed = 5f;
void Update() {
height += Input.GetAxis("$$anonymous$$ouse ScrollWheel") * scrollSpeed;
height = $$anonymous$$athf.Clamp(height, height$$anonymous$$in, height$$anonymous$$ax);
}
Outstanding! Thanks again mate!
Answer by sonofatrus · Jun 25, 2012 at 07:07 PM
Maybe should to manipulate with FOV when non-orthographic camera mode?
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Advanced movement and camera controls (Angrybots-ish) 0 Answers
RPG scripts 2 Answers
Smooth Follow Camera Rotate on Z-Axis? 2 Answers