using UnityEngine;
using HedgehogTeam.EasyTouch;
public class TouchManager : MonoBehaviour
{
//上次鼠标位置
Vector2 prevMousePos = Vector3.zero;
//滑动结束时的瞬时速度
Vector3 Speed = Vector3.zero;
//每帧偏差
Vector3 offSet = Vector3.zero;
//鼠标开始位置
Vector3 startMousePosition = Vector3.zero;
//速度衰減率
public float decelerationRate = 0.1f;
//摄像机
public Camera m_camera;
void Update()
{
HandleMouseInput();
}
private void HandleMouseInput()
{
//按下时记录位置
if (Input.GetMouseButtonDown(0))
{
prevMousePos = Input.mousePosition;
startMousePosition = Input.mousePosition;
}
//移动时更新位置
if (Input.GetMouseButton(0))
{
Vector3 curMousePosition = Input.mousePosition; //当前鼠标的屏幕坐标系
//偏差值
offSet = m_camera.ScreenToWorldPoint(curMousePosition) - m_camera.ScreenToWorldPoint(prevMousePos);
prevMousePos = curMousePosition;
//瞬时速度
Speed = offSet / Time.deltaTime;
}
else //最后递减
{
Speed *= Mathf.Pow(decelerationRate, Time.deltaTime);
if (Mathf.Abs(Vector3.Magnitude(Speed)) < 1)
{
Speed = Vector3.zero;
}
}
Move(Speed);
}
public void Move(Vector3 speed)
{
if (Vector3.Magnitude(Speed) == 0)
{
return;
}
m_camera.transform.localPosition -= speed * Time.deltaTime;
}
}