61 lines
1.5 KiB
C#
61 lines
1.5 KiB
C#
using UnityEngine;
|
|
|
|
public class ZoomCamera : MonoBehaviour
|
|
{
|
|
public Transform origin;
|
|
|
|
public float zoom;
|
|
|
|
public float zoomMin = -5f;
|
|
|
|
public float zoomMax = 5f;
|
|
|
|
public float seekTime = 1f;
|
|
|
|
public bool smoothZoomIn;
|
|
|
|
private Vector3 defaultLocalPosition;
|
|
|
|
private float currentZoom;
|
|
|
|
private float targetZoom;
|
|
|
|
private float zoomVelocity;
|
|
|
|
private void Start()
|
|
{
|
|
defaultLocalPosition = base.transform.localPosition;
|
|
currentZoom = zoom;
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
zoom = Mathf.Clamp(zoom, zoomMin, zoomMax);
|
|
int layerMask = -261;
|
|
Vector3 position = origin.position;
|
|
Vector3 position2 = defaultLocalPosition + base.transform.parent.InverseTransformDirection(base.transform.forward * zoom);
|
|
Vector3 end = base.transform.parent.TransformPoint(position2);
|
|
RaycastHit hitInfo;
|
|
if (Physics.Linecast(position, end, out hitInfo, layerMask))
|
|
{
|
|
Vector3 vector = hitInfo.point + base.transform.TransformDirection(Vector3.forward);
|
|
targetZoom = (vector - base.transform.parent.TransformPoint(defaultLocalPosition)).magnitude;
|
|
}
|
|
else
|
|
{
|
|
targetZoom = zoom;
|
|
}
|
|
targetZoom = Mathf.Clamp(targetZoom, zoomMin, zoomMax);
|
|
if (!smoothZoomIn && targetZoom - currentZoom > 0f)
|
|
{
|
|
currentZoom = targetZoom;
|
|
}
|
|
else
|
|
{
|
|
currentZoom = Mathf.SmoothDamp(currentZoom, targetZoom, ref zoomVelocity, seekTime);
|
|
}
|
|
position2 = defaultLocalPosition + base.transform.parent.InverseTransformDirection(base.transform.forward * currentZoom);
|
|
base.transform.localPosition = position2;
|
|
}
|
|
}
|