RaisableCamera
In third person mode, I wanted to be able to raise and lower the camera with mouse wheel. This script attached to the camera makes that happen.
/* *
* Attach to a camera. Adds using mouse scroll wheel to raise/lower camera.
* */
using UnityEngine;
using System.Collections;
public class RaisableCamera : MonoBehaviour {
public float maxHeight = 10f; // No higher than this.
public float minHeight = 1f; // No closer than this.
public float direction = 40f; // Angle to raise: <0 = ahead, >0 = behind.
public float sensitivity = 2f;
private Transform cameraTransform;
void Awake () {
cameraTransform = transform;
}
void Update () {
float dy = Input.GetAxis("Mouse ScrollWheel") * sensitivity;
if (dy != 0) {
// Clamp to height.
if (dy + cameraTransform.localPosition.y > maxHeight) {
dy = maxHeight - cameraTransform.localPosition.y;
}
if (dy + cameraTransform.localPosition.y < minHeight) {
dy = minHeight - cameraTransform.localPosition.y;
}
}
if (dy != 0) {
float dz = Mathf.Tan(Mathf.Deg2Rad * direction) * dy;
Vector3 dv = new Vector3(0, dy, -dz);
cameraTransform.Translate(dv, cameraTransform.parent);
}
}
}
Recent Comments