IOS触摸
using UnityEngine;
using System.Collections;
public class MyIOSTouch : MonoBehaviour {
// 参照物
public GameObject target;
// 相机与参照物之间距离
public float distance = 10.0f;
// 滑动速度
public float xSpeed = 250.0f;
public float ySpeed = 120.0f;
// 角度限制
public float yMinLimit = -20.0f;
public float yMaxLimit = 80.0f;
// 距离限制
public float disMin = 3.0f;
public float disMax = 18.5f;
private float angleX = 0.0f;
private float angleY = 0.0f;
private Vector2 oldPosition_1;
private Vector2 oldPosition_2;
void Start ()
{
angleX = transform.eulerAngles.x;
angleY = transform.eulerAngles.y;
}
void Update ()
{
if (1 == Input.touchCount)
{
if( Input.GetTouch( 0 ).phase == TouchPhase.Moved )
{
angleX += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
angleY -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;
}
}
else if ( 1 < Input.touchCount )
{
if( Input.GetTouch( 0 ).phase == TouchPhase.Moved || Input.GetTouch( 1 ).phase == TouchPhase.Moved )
{
Vector2 _tmp_1 = Input.GetTouch( 0 ).position;
Vector2 _tmp_2 = Input.GetTouch( 1 ).position;
if( isEnlarge( oldPosition_1, oldPosition_2, _tmp_1, _tmp_2) )
{
if( distance > disMin )
{
distance -= 0.5f;
}
}
else
{
if( distance < disMax )
{
distance += 0.5f;
}
}
oldPosition_1 = _tmp_1;
oldPosition_2 = _tmp_2;
}
}
}
void LateUpdate()
{
if (target)
{
angleY = ClampAngle( angleY, yMinLimit, yMaxLimit );
transform.rotation = Quaternion.Euler( angleY, angleX, 0.0f );
transform.position = transform.rotation * new Vector3( 0.0f, 0.0f, -1.0f * distance ) + target.transform.position;
}
}
bool isEnlarge( Vector2 ov1, Vector2 ov2, Vector2 nv1, Vector2 nv2 )
{
float oLen = ( ov1.x - ov2.x ) * ( ov1.x - ov2.x ) + ( ov1.y - ov2.y ) * ( ov1.y - ov2.y );
float nLen = ( nv1.x - nv2.x ) * ( nv1.x - nv2.x ) + ( nv1.y - nv2.y ) * ( nv1.y - nv2.y );
if (oLen < nLen)
{
return true;
}
else
{
return false;
}
}
float ClampAngle( float angle, float min, float max )
{
if (-360 > angle)
{
angle += 360;
}
if (360 < angle)
{
angle -= 360;
}
return Mathf.Clamp (angle, min, max);
}
}