unity3d 触屏多点触控(旋转与缩放)

Java代码 复制代码 收藏代码
  1. /*
  2. Touch Orbit
  3. Programmed by: Randal J. Phillips (Caliber Mengsk)
  4. Original Creation Date: 12/16/2011
  5. Last Updated: 12/16/2011
  6. Desctiption: Simple orbit by one touch and drag, as well as pinch to zoom with two fingers.
  7. */
  8. var x:float;
  9. var y:float;
  10. var xSpeed:float;
  11. var ySpeed:float;
  12. var pinchSpeed:float;
  13. var distance:float = 10;
  14. var minimumDistance:float = 5;
  15. var maximumDistance:float = 100;
  16. private var touch:Touch;
  17. private var lastDist:float = 0;
  18. private var curDist:float = 0;
  19. private var gameCamera:Camera;
  20. function Start ()
  21. {
  22. gameCamera = Camera.mainCamera;
  23. }
  24. function Update ()
  25. {
  26. if (Input.GetKeyDown(KeyCode.Escape))
  27. {
  28. Application.Quit();
  29. }
  30. if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
  31. {
  32. //One finger touch does orbit
  33. touch = Input.GetTouch(0);
  34. x += touch.deltaPosition.x * xSpeed * 0.02;
  35. y -= touch.deltaPosition.y * ySpeed * 0.02;
  36. }
  37. if (Input.touchCount > 1 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(1).phase == TouchPhase.Moved))
  38. {
  39. //Two finger touch does pinch to zoom
  40. var touch1 = Input.GetTouch(0);
  41. var touch2 = Input.GetTouch(1);
  42. curDist = Vector2.Distance(touch1.position, touch2.position);
  43. if(curDist > lastDist)
  44. {
  45. distance += Vector2.Distance(touch1.deltaPosition, touch2.deltaPosition)*pinchSpeed/10;
  46. }else{
  47. distance -= Vector2.Distance(touch1.deltaPosition, touch2.deltaPosition)*pinchSpeed/10;
  48. }
  49. lastDist = curDist;
  50. }
  51. if(distance <= minimumDistance)
  52. {
  53. //minimum camera distance
  54. distance = minimumDistance;
  55. }
  56. if(distance >= maximumDistance)
  57. {
  58. //maximum camera distance
  59. distance = maximumDistance;
  60. }
  61. //Sets rotation
  62. var rotation = Quaternion.Euler(y, x, 0);
  63. //Sets zoom
  64. var position = rotation * Vector3(0.0, 0.0, -distance) + Vector3(0,0,0);
  65. //Applies rotation and position
  66. transform.rotation = rotation;
  67. transform.position = position;
  68. }
  69. function OnGUI()
  70. {
  71. //Simple output to display the distance from the center
  72. GUI.Label(Rect(0,0,Screen.width, Screen.height),distance.ToString());
  73. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值