- Home /
Is it possible to add zoom to the mouse orbit script?
As the above states...
I want to control it with the mouse wheel/scroll. (it is added to my 3rd person camera, my game switches between 1st and 3rd)
Answer by Matthew Crawford · Dec 09, 2010 at 07:45 PM
Yes it's possible. Just detect the scrollwheel input and move the camera object as needed. This might point you in the right direction; http://www.unifycommunity.com/wiki/index.php?title=MouseOrbitZoom
i got it working but there is no limit on the x axes i can view 360 degree and still walk forward with it(whilst facing backward) :s
Answer by deadshot · Oct 20, 2014 at 06:48 AM
for zoom
using UnityEngine; using System.Collections;
public class zoom : MonoBehaviour {
public float zoomSensitivity= 15.0f;
public float zoomSpeed= 5.0f;
public float zoomMin= 5.0f;
public float zoomMax= 80.0f;
private float z;
void Start() {
z = camera.fieldOfView;
}
void Update() {
z -= Input.GetAxis("Mouse ScrollWheel") * zoomSensitivity;
z = Mathf.Clamp(z, zoomMin, zoomMax);
}
void LateUpdate() {
camera.fieldOfView = Mathf.Lerp (camera.fieldOfView, z, Time.deltaTime * zoomSpeed);
}
}
for mouse orbit on right click
using UnityEngine; using System.Collections;
public class rotateonmouse : MonoBehaviour { public float speed = 5.0f; public Transform target ; public float Distance = 5.0f; public float xSpeed = 250.0f; public float ySpeed = 120.0f; public float yMinLimit = -20.0f; public float yMaxLimit = 80.0f;
private float x;
private float y;
void Awake()
{
Vector3 angles = transform.eulerAngles;
x = angles.x;
y = angles.y;
if(GetComponent<Rigidbody>() != null)
{
rigidbody.freezeRotation = true;
}
}
void LateUpdate()
{
if (Input.GetMouseButton(1)) {
transform.LookAt(target);
transform.RotateAround(target.position, Vector3.up, Input.GetAxis("Mouse X")*speed);
if(target != null)
{
x += (float)(Input.GetAxis("Mouse X") * xSpeed * 0.02f);
y -= (float)(Input.GetAxis("Mouse Y") * ySpeed * 0.02f);
y = ClampAngle(y, yMinLimit, yMaxLimit);
Quaternion rotation = Quaternion.Euler(y, x, 0);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, 100.0f * Time.deltaTime);
Vector3 position = rotation * (new Vector3(0.0f, 0.0f, -Distance)) + target.position;
transform.rotation = rotation;
transform.position = position;
}
}
}
private float ClampAngle(float angle, float min, float max)
{
if(angle < -360)
{
angle += 360;
}
if(angle > 360)
{
angle -= 360;
}
return Mathf.Clamp (angle, min, max);
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
} }
Your answer
![](https://koobas.hobune.stream/wayback/20220613090415im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mouse Wheel Zoom code not working like it should 1 Answer
Character Zoom in and out 1 Answer
what is the name of the mouse wheel in the input 2 Answers
Zooming To Mouse Position With Scroll Wheel 3 Answers
ORBIT CAMERA ZOOM LIMIT 0 Answers