using UnityEngine;
using System.Collections;
public class CamCtrl : MonoBehaviour
{
static public CamCtrl instance;
Vector3 camPosInit;
public Transform transFollowTarget = null;
void Awake()
{
instance = this;
zoomDist = Mathf.Abs(transCam.localPosition.z);
}
void Start()
{
MouseInput.instance.leftDragDeltaEvent += RotManual;
MouseInput.instance.leftRelaxedEvent += RotAuto;
//
MouseInput.instance.middleDragDeltaEvent += MoveManual;
MouseInput.instance.middleRelaxedEvent += MoveAuto;
//
MouseInput.instance.mouseScrollEvent += Zoom;
camPosInit = transform.position;
zoomTarget = transCam.localPosition;
}
// Update is called once per frame
void Update()
{
ZoomFollow();
}
/// <summary>
///
/// </summary>
float rotSpeed = 0.5f;
void RotManual(Vector2 deltaPos)
{
Vector3 euler = transform.localEulerAngles;
euler.x -= deltaPos.y * rotSpeed;
euler.y += deltaPos.x * rotSpeed;
euler.x = Mathf.Clamp(euler.x, 45, 90);
transform.localEulerAngles = euler;
}
float rotLerpSpeed = 3;
float angleYTarget = 0;
public float AngleYTarget { get { return angleYTarget; } }
void RotAuto(Vector2 deltaPos)
{
Vector3 euler = transform.localEulerAngles;
euler.y = Mathf.LerpAngle(euler.y, angleYTarget, rotLerpSpeed * Time.deltaTime);
transform.localEulerAngles = euler;
}
/// <summary>
///
/// </summary>
float moveSpeed = 0.003f;
void MoveManual(Vector2 deltaPos)
{
Vector3 move = new Vector3(-deltaPos.x, -deltaPos.y, 0);
move *= moveSpeed;
move *= Mathf.Abs(transCam.localPosition.z);
transform.Translate(move);
Vector3 pos = transform.position;
pos.y = camPosInit.y;
transform.position = pos;
}
float moveLerpSpeed = 6;
void MoveAuto(Vector2 deltaPos)
{
Vector3 targetPos = transFollowTarget == null ? camPosInit : transFollowTarget.position;
transform.position = Vector3.Lerp(transform.position, targetPos, moveLerpSpeed * Time.deltaTime);
}
/// <summary>
///
/// </summary>
public Transform transCam;
float scrollSpeed = 15;
Vector3 zoomTarget;
float zoomLerpSpeed = 5;
void Zoom(float scroll)
{
Vector3 pos = transCam.localPosition;
pos.z += scroll * scrollSpeed;
pos.z = Mathf.Clamp(pos.z, -60, -10);
zoomTarget = pos;
}
void ZoomFollow()
{
transCam.localPosition = Vector3.Lerp(transCam.localPosition, zoomTarget, zoomLerpSpeed * Time.deltaTime);
}
float zoomDist;
public float ZoomDist { get { return zoomDist; } }
public void SetCamCtrl(Transform transFollowTarget, float angleYTarget, float dist)
{
zoomDist = dist;
this.transFollowTarget = transFollowTarget;
this.angleYTarget = angleYTarget;
zoomTarget = new Vector3(0, 0, -dist);
}
}
摄像机旋转移动
最新推荐文章于 2020-01-19 17:40:59 发布