using UnityEngine;
using System.Collections;
public class CamMove : MonoBehaviour
{
private float m_xSpeed = 250.0f;
private float m_ySpeed = 120.0f;
private float m_yMinLimit = -45;
private float m_yMaxLimit = 45;
private float m_xAngles = 0.0f;
private float m_yAngles = 0.0f;
Vector2 m_oldPos0;
Vector2 m_oldPos1;
public float m_scale = 1.0f;
float m_scaleStep = 0.1f;
float m_scaleMax = 2.0f;
public bool m_enable = true;
void Start()
{
Vector3 angles = transform.eulerAngles;
m_xAngles = angles.y;
m_yAngles = angles.x;
}
void Update()
{
if (Application.platform == RuntimePlatform.WindowsEditor)
{
if (m_enable)
{
if (Input.GetMouseButton(0))
{
m_xAngles -= Input.GetAxis("Mouse X") * m_xSpeed * 0.02f;
m_yAngles += Input.GetAxis("Mouse Y") * m_ySpeed * 0.02f;
}
else
{
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
FullRenderScale(false, 0.1f);
}
//Zoom in
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
FullRenderScale(true, 0.1f);
}
}
}
}
else
{
if (m_enable)
{
if (Input.touchCount == 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved)
{
m_xAngles -= Input.GetAxis("Mouse X") * m_xSpeed * 0.02f;
m_yAngles += Input.GetAxis("Mouse Y") * m_ySpeed * 0.02f;
}
}
else if (Input.touchCount > 1)
{
if (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved)
{
var curPos0 = Input.GetTouch(0).position;
var curPos1 = Input.GetTouch(1).position;
if (IsEnlarge(m_oldPos0, m_oldPos1, curPos0, curPos1))
{
FullRenderScale(true, 0.01f);
}
else
{
FullRenderScale(false, 0.01f);
}
m_oldPos0 = curPos0;
m_oldPos1 = curPos1;
}
}
}
}
}
bool IsEnlarge(Vector2 old0,Vector2 old1,Vector2 cur0,Vector2 cur1)
{
var leng1 = Mathf.Sqrt((old0.x - old1.x) * (old0.x - old1.x) + (old0.y - old1.y) * (old0.y - old1.y));
var leng2 = Mathf.Sqrt((cur0.x - cur1.x) * (cur0.x - cur1.x) + (cur0.y - cur1.y) * (cur0.y - cur1.y));
if(leng1<leng2)
{
return true;
}
else
{
return false;
}
}
void LateUpdate()
{
if (m_enable)
{
m_yAngles = ClampAngle(m_yAngles, m_yMinLimit, m_yMaxLimit);
Quaternion rotation = Quaternion.Euler(m_yAngles, m_xAngles, 0);
transform.rotation = rotation;
}
}
static float ClampAngle(float angle, float min, float max)
{
if (angle < -360)
angle += 360;
if (angle > 360)
angle -= 360;
return Mathf.Clamp(angle, min, max);
}
void FullRenderScale(bool isBig,float scale)
{
if (isBig)
{
if (m_scale <= m_scaleMax)
{
m_scale += scale;
}
}
else
{
if (m_scale > 1.0f)
{
m_scale -= scale;
}
}
MainMgr.Instance.m_fullRender.transform.localScale = new Vector3(m_scale, m_scale, m_scale);
}
private void OnEnable()
{
m_enable = true;
}
}
[Unity]拖动摄像机视角与双指缩放
于 2023-11-14 19:06:45 首次发布