纳金网
标题:
通过手势拉近相机
[打印本页]
作者:
烟雨
时间:
2015-5-29 02:23
标题:
通过手势拉近相机
using UnityEngine;
using System.Collections;
// 输入管理类, 统一鼠标点击和触屏操作。
public class InputM : MonoBehaviour
{
// 鼠标位置
public static Vector3 mousePosition;
// 鼠标单击
private static bool isMouseButtonDown = false;
public static bool GetMouseButtonDown()
{
return isMouseButtonDown;
}
// 鼠标长按时间
private static bool isMouseButton = false;
public static bool GetMouseButton()
{
return isMouseButton;
}
// 滚轮
private static float mouseScrollWheelAxis = 0f;
public static float GetMouseScrollWheelAxis()
{
return mouseScrollWheelAxis;
}
#if UNITY_ANDROID || UNITY_IPHONE
private float scrollValue = 0.02f; // 触屏两点模拟滚轮
private Vector2 pos0, pos1;
private Touch[] touch;
#endif
void Update()
{
#if UNITY_ANDROID || UNITY_IPHONE
isMouseButtonDown = Input.GetMouseButtonDown(0);
isMouseButton = Input.GetMouseButton (0);
mousePosition = Input.mousePosition;
touch = Input.touches;
if (touch.Length < 2)
{
mouseScrollWheelAxis = 0;
}
else
{
Vector2 newPos0 = touch[0].position, newPos1 = touch[1].position;
if (touch[0].phase != TouchPhase.Began && touch[1].phase != TouchPhase.Began)
{
float xOldDis = Mathf.Abs(pos0.x - pos1.x), yOldDis = Mathf.Abs(pos0.y - pos1.y)
, xDis = Mathf.Abs(newPos0.x - newPos1.x), yDis = Mathf.Abs(newPos0.y - newPos1.y);
mouseScrollWheelAxis = (xOldDis - xDis + yOldDis - yDis) * scrollValue;
}
pos0 = newPos0;
pos1 = newPos1;
}
#else
isMouseButtonDown = Input.GetMouseButtonDown(0);
isMouseButton = Input.GetMouseButton (0);
mousePosition = Input.mousePosition;
mouseScrollWheelAxis = Input.GetAxis ("Mouse ScrollWheel");
#endif
}
}
复制代码
需要变化的相机上加上
void Update()
{
float fov = pCamera.fieldOfView;
fov += InputM.GetMouseScrollWheelAxis() * sensitivity;
fov = Mathf.Clamp(fov, minFov, maxFov);
pCamera.fieldOfView = fov;
}
复制代码
欢迎光临 纳金网 (http://go.narkii.com/club/)
Powered by Discuz! X2.5