本帖最后由 我是一头小毛驴 于 2016-2-26 20:45 编辑
//用于绑定参照物对象 var target : Transform; //缩放系数 var distance = 10.0; //左右滑动移动速度 var xSpeed = 250.0; var ySpeed = 120.0; //缩放限制系数 var yMinLimit = -20; var yMaxLimit = 80; //摄像头的位置 var x = 0.0; var y = 0.0; //记录上一次手机触摸位置判断用户是在左放大还是缩小手势 private var oldPosition1 : Vector2; private var oldPosition2 : Vector2;
//初始化游戏信息设置 function Start () { var angles = transform.eulerAngles; x = angles.y; y = angles.x;
// Make the rigid body not change rotation if (rigidbody) rigidbody.freezeRotation = true; }
function Update () { //判断触摸数量为单点触摸 if(Input.touchCount == 1) { //触摸类型为移动触摸 if(Input.GetTouch(0).phase==TouchPhase.Moved) { //根据触摸点计算X与Y位置 x += Input.GetAxis("Mouse X") * xSpeed * 0.02; y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02; } } //判断触摸数量为多点触摸 if(Input.touchCount >1 ) { //前两只手指触摸类型都为移动触摸 if(Input.GetTouch(0).phase==TouchPhase.Moved??Input.GetTouch(1).phase==TouchPhase.Moved) { //Unity3d教程.html" target="_blank" class="relatedlink">Unity3d教程手册:www.manew.com //计算出当前两点触摸点的位置 var tempPosition1 = Input.GetTouch(0).position; var tempPosition2 = Input.GetTouch(1).position; //函数返回真为放大,返回假为缩小 if(isEnlarge(oldPosition1,oldPosition2,tempPosition1,tempPosition2)) { //放大系数超过3以后不允许继续放大 //这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改 if(distance > 3) { distance -= 0.5; } }else{ //缩小洗漱返回18.5后不允许继续缩小 //这里的数据是根据我项目中的模型而调节的,大家可以自己任意修改 if(distance < 18.5) { distance += 0.5; } } //备份上一次触摸点的位置,用于对比 oldPosition1=tempPosition1; oldPosition2=tempPosition2; } } } //函数返回真为放大,返回假为缩小 function isEnlarge(oP1 : Vector2,oP2 : Vector2,nP1 : Vector2,nP2 : Vector2) : boolean { //函数传入上一次触摸两点的位置与本次触摸两点的位置计算出用户的手势 var leng1 =Mathf.Sqrt((oP1.x-oP2.x)*(oP1.x-oP2.x)+(oP1.y-oP2.y)*(oP1.y-oP2.y)); var leng2 =Mathf.Sqrt((nP1.x-nP2.x)*(nP1.x-nP2.x)+(nP1.y-nP2.y)*(nP1.y-nP2.y)); if(leng1<leng2) { //放大手势 return true; }else{ //缩小手势 return false; } } //Update方法一旦调用结束以后进入这里算出重置摄像机的位置 function LateUpdate () { //target为我们绑定的箱子变量,缩放旋转的参照物 if (target) { //重置摄像机的位置 y = ClampAngle(y, yMinLimit, yMaxLimit); var rotation = Quaternion.Euler(y, x, 0); var position = rotation * Vector3(0.0, 0.0, -distance) + target.position; transform.rotation = rotation; transform.position = position; } } static function ClampAngle (angle : float, min : float, max : float) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp (angle, min, max); }
C#版本并加上y限制。 using UnityEngine; using System.Collections; public class Move : MonoBehaviour { public GameObject target; public float distance = 10.0f; public float xSpeed = 250.0f; public float ySpeed = 120.0f; Vector3 tmp; public float yMinLimit = -20; public float yMaxLimit = 80; public float x = 0.0f; public float y = 0.0f; private Vector2 oldPosition1; private Vector2 oldPosition2;
void Start (){ Vector2 angles= transform.eulerAngles; x = angles.y; y = angles.x;
// Make the rigid body not change rotation if (rigidbody) rigidbody.freezeRotation = true; }
void Update (){ if(Input.touchCount == 1) { if(Input.GetTouch(0).phase==TouchPhase.Moved) { x += Input.GetAxis("Mouse X") * xSpeed * 0.02f; y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f; y=ClampAngle(y,yMinLimit,yMaxLimit); } } if(Input.touchCount >1 ) { if(Input.GetTouch(0).phase==TouchPhase.Moved||Input.GetTouch(1).phase==TouchPhase.Moved) { Vector3 tempPosition1= Input.GetTouch(0).position; Vector3 tempPosition2= Input.GetTouch(1).position; if(isEnlarge(oldPosition1,oldPosition2,tempPosition1,tempPosition2)) { if(distance > 3) { distance -= 0.5f; } }else{ if(distance < 18.5f) { distance += 0.5f; } } oldPosition1=tempPosition1; oldPosition2=tempPosition2; } } } bool isEnlarge ( Vector2 oP1 , Vector2 oP2 , Vector2 nP1 , Vector2 nP2 ) { float leng1=Mathf.Sqrt((oP1.x-oP2.x)*(oP1.x-oP2.x)+(oP1.y-oP2.y)*(oP1.y-oP2.y)); float leng2=Mathf.Sqrt((nP1.x-nP2.x)*(nP1.x-nP2.x)+(nP1.y-nP2.y)*(nP1.y-nP2.y)); if(leng1<leng2) { return true; }else{ return false; } }
public void LateUpdate (){ if (target) { ClampAngle(y, yMinLimit, yMaxLimit); Quaternion rotation= Quaternion.Euler(y, x, 0); tmp.Set(0.0f, 0.0f, (-1)*distance); Vector3 position= rotation * tmp + target.transform.position; transform.rotation = rotation; transform.position = position; } } 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);
} } |
触摸屏手势控制镜头旋转与缩放
最新推荐文章于 2023-08-29 12:16:17 发布