unity实现鼠标拖拽缩放功能代码

来源:互联网 发布:北京知果科技怎么样 编辑:程序博客网 时间:2024/05/18 20:36
  1. using UnityEngine;
  2. using System.Collections;

  3. public class Script_07_11 : MonoBehaviour 
  4. {

  5. public Transform target;
  6. private int MouseWheelSensitivity = 1;
  7. private int MouseZoomMin = 1;
  8. private int MouseZoomMax = 5;
  9. private float normalDistance = 3;

  10. private Vector3 normalized;

  11. private float xSpeed = 250.0f;
  12. private float ySpeed = 120.0f;

  13. private int yMinLimit = -20;
  14. private int yMaxLimit = 80;

  15. private float x = 0.0f;
  16. private float y = 0.0f;

  17. private Vector3 screenPoint;
  18. private Vector3 offset;

  19. private Quaternion rotation = Quaternion.Euler(new Vector3(30f,0f,0f));
  20. private Vector3 CameraTarget;
  21. void Start () 
  22. {

  23. CameraTarget = target.position;

  24. float z = target.transform.position.z - normalDistance;
  25. transform.position =  rotation * new Vector3(transform.position.x,transform.position.y,z);

  26. transform.LookAt(target);

  27. var angles = transform.eulerAngles;
  28.      x = angles.y;
  29.      y = angles.x;
  30. }

  31. void Update ()
  32. {

  33. if(Input.GetMouseButton(1))
  34. {
  35.      x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
  36.          y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

  37.   y = ClampAngle(y, yMinLimit, yMaxLimit);

  38.          var rotation = Quaternion.Euler(y, x, 0);
  39.          var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + CameraTarget;

  40.          transform.rotation = rotation;
  41.          transform.position = position;

  42. }else if (Input.GetAxis("Mouse ScrollWheel") != 0)
  43. {
  44.      normalized = (transform.position - CameraTarget).normalized;

  45. if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax)
  46. {
  47. normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;
  48. }
  49. if (normalDistance < MouseZoomMin)
  50. {
  51. normalDistance = MouseZoomMin;
  52. }
  53. if (normalDistance > MouseZoomMax)
  54. {
  55. normalDistance = MouseZoomMax;
  56. }
  57. transform.position =    normalized * normalDistance;

  58. }else if(Input.GetMouseButtonDown(2))
  59. {
  60.       screenPoint = Camera.main.WorldToScreenPoint(target.transform.position);
  61.      offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
  62. }
  63. //unity3d:http://www.unitymanual.com/
  64. if(Input.GetMouseButton(2))
  65. {
  66.    Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);

  67. Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
  68.   target.transform.position = curPosition;
  69. }
  70. transform.LookAt(CameraTarget);

  71. }

  72. static float ClampAngle (float angle , float min ,float  max) 
  73. {
  74. if (angle < -360)
  75. angle += 360;
  76. if (angle > 360)
  77. angle -= 360;
  78. return Mathf.Clamp (angle, min, max);
  79. }