using UnityEngine;
using System.Collections;
public class CamManualCtrl : MonoBehaviour
{
static public CamManualCtrl instance;
Vector3 camPosInit;
public Transform transFollowTarget = null;
void Awake()
{
instance = this;
zoomDist = Mathf.Abs(CamManualRootParent.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 = CamManualRootParent.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, 5, 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.unscaledDeltaTime);
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(CamManualRootParent.localPosition.z);
transform.Translate(move);
Vector3 pos = transform.position;
//这里使用targetPos.y应该更合适
pos.y = targetPos.y;
transform.position = pos;
}
float moveLerpSpeed = 6;
Vector3 targetPos;
void MoveAuto(Vector2 deltaPos)
{
//这里的targetPos应该定义到外面,方便在MoveManual方法里面使用
targetPos = transFollowTarget == null ? camPosInit : transFollowTarget.position;
transform.position = Vector3.Lerp(transform.position, targetPos, moveLerpSpeed * Time.unscaledDeltaTime);
}
/// <summary>
///
/// </summary>
public Transform CamManualRootParent;
float scrollSpeed = 15;
Vector3 zoomTarget;
float zoomLerpSpeed = 5;
void Zoom(float scroll)
{
Vector3 pos = CamManualRootParent.localPosition;
pos.z += scroll * scrollSpeed;
pos.z = Mathf.Clamp(pos.z, -10, -3);
zoomTarget = pos;
}
void ZoomFollow()
{
CamManualRootParent.localPosition = Vector3.Lerp(CamManualRootParent.localPosition, zoomTarget, zoomLerpSpeed * Time.unscaledDeltaTime);
}
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);
}
}
//命名空间
using UnityEngine;
using System.Collections;
namespace CarState {
public delegate void setModelHandler();
public delegate void mousePosHandler(Vector2 pos);
public delegate void MouseScrollHanlder(float scroll);
public delegate void FlowStep();
public delegate void PlayHanlder();
public enum ShowState
{
manual,
auto
}
public enum ShowOperate
{
sceneRot,
sceneMove,
zoom,
move,
rotate
}
}
//
using UnityEngine;
using System.Collections;
using CarState;
using UnityEngine.EventSystems;
public class MouseInput : MonoBehaviour
{
static public MouseInput instance;
void Awake()
{
instance = this;
}
void Start()
{
}
//鼠标左键事件/
public event mousePosHandler leftDownEvent;
public event mousePosHandler leftUpEvent;
public event mousePosHandler leftDragEvent;
public event mousePosHandler leftDragDeltaEvent;
public event mousePosHandler leftRelaxedEvent;
public event mousePosHandler leftClickEvent;
Vector2 posLeftDown;
Vector2 posLeftPre;
bool isLeftPressed = false;
bool clickEnabled = true;
//鼠标中键事件/
public event mousePosHandler middleDownEvent;
public event mousePosHandler middleUpEvent;
public event mousePosHandler middleDragEvent;
public event mousePosHandler middleDragDeltaEvent;
public event mousePosHandler middleRelaxedEvent;
Vector2 posMiddlePre;
bool isMiddlePressed = false;
bool isMouseOnUI = false;
//鼠标滚轮事件/
public event MouseScrollHanlder mouseScrollEvent;
void Update()
{
isMouseOnUI = EventSystem.current.IsPointerOverGameObject();
//鼠标左键事件/
if (Input.GetMouseButtonDown(0))
{
if (isMouseOnUI) return;
isLeftPressed = true;
clickEnabled = true;
posLeftDown = Input.mousePosition;
posLeftPre = Input.mousePosition;
if (leftDownEvent != null) leftDownEvent(Input.mousePosition);
}
if(isLeftPressed)
{
if(((Vector2)Input.mousePosition - posLeftDown).sqrMagnitude > 9)
{
clickEnabled = false;
}
if(!clickEnabled)
{
if (leftDragEvent != null) leftDragEvent(Input.mousePosition);
Vector2 leftDragDelta = (Vector2)Input.mousePosition - posLeftPre;
posLeftPre = Input.mousePosition;
if (leftDragDeltaEvent != null) leftDragDeltaEvent(leftDragDelta);
}
}
else
{
if (leftRelaxedEvent != null) leftRelaxedEvent(Input.mousePosition);
}
if(Input.GetMouseButtonUp(0))
{
isLeftPressed = false;
if (leftUpEvent != null) leftUpEvent(Input.mousePosition);
if (!isMouseOnUI)
{
if (clickEnabled)
{
if (leftClickEvent != null) leftClickEvent(Input.mousePosition);
}
}
}
//鼠标中键事件/
if (Input.GetMouseButtonDown(2))
{
if (isMouseOnUI) return;
isMiddlePressed = true;
posMiddlePre = Input.mousePosition;
if (middleDownEvent != null) middleDownEvent(Input.mousePosition);
}
if (isMiddlePressed)
{
if (middleDragEvent != null) middleDragEvent(Input.mousePosition);
Vector2 middleDragDelta = (Vector2)Input.mousePosition - posMiddlePre;
posMiddlePre = Input.mousePosition;
if (middleDragDeltaEvent != null) middleDragDeltaEvent(middleDragDelta);
}
else
{
if (middleRelaxedEvent != null) middleRelaxedEvent(Input.mousePosition);
}
if (Input.GetMouseButtonUp(2))
{
isMiddlePressed = false;
if (middleUpEvent != null) middleUpEvent(Input.mousePosition);
}
//鼠标滚轮事件/
float scroll = Input.GetAxis("Mouse ScrollWheel");
if(!Mathf.Approximately(scroll ,0))
{
if (mouseScrollEvent != null) mouseScrollEvent(scroll);
}
}
}
摄像机的旋转,移动,缩放
最新推荐文章于 2024-04-10 20:01:35 发布