Unity C# 一些关于Camera的心得!

2018-06-18 04:15:18来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

本文原创,转载请注明出处:http://www.cnblogs.com/AdvancePikachu/p/6856374.html

首先,总结了下最近工作中关于摄像机漫游的功能,

脚本如下:

  1     Transform _Camera;
  2     public LayerMask mask;
  3      
  4     public float checkHeight = 500f;
  5     public float minHeight = 20f;
  6     public float maxHeight = 8000f;
  7     public float minClamp = 50f;
  8     public float maxClamp = 950f;
  9 
 10     public float sensitivityX = 5f;
 11     public float sensitivityY = 5f;
 12     private float rotationY = 0f;
 13 
 14     //上下最大Y视角
 15     public float minimumY = -90f;
 16     public float maximumY = 30f;
 17 
 18     public Vector3 PreMouseMPos;
 19 
 20     public float scrollSpeed = 200f;
 21 
 22     void Start () 
 23     {
 24         mask.value = 1;
 25         _Camera = Camera.main.transform;
 26     }
 27     
 28     // Update is called once per frame
 29     void Update ()
 30     {
 31         if(Input.GetKey(KeyCode.LeftAlt))
 32         {
 33             MouseScrollWheel ();
 34             CameraMove ();
 35             MoveEulerAngles ();
 36         }
 37     }
 38 
 39     /// <summary>
 40     /// Checks the height of the low.
 41     /// </summary>
 42     void CheckLowHeight()
 43     {
 44         if (_Camera.position.x < minClamp)
 45             _Camera.position = new Vector3 (minClamp, _Camera.position.y, _Camera.position.z);
 46         if (_Camera.position.x > maxClamp)
 47             _Camera.position = new Vector3 (maxClamp, _Camera.position.y, _Camera.position.z);
 48         if (_Camera.position.z < minClamp)
 49             _Camera.position = new Vector3 (_Camera.position.x, _Camera.position.y, minClamp);
 50         if (_Camera.position.z > maxClamp)
 51             _Camera.position = new Vector3 (_Camera.position.x, _Camera.position.y, maxClamp);
 52 
 53         RaycastHit hit;
 54         if(Physics.Raycast(_Camera.position+Vector3.up*checkHeight,Vector3.down,out hit ,checkHeight+minHeight,mask))
 55         {
 56             if(_Camera.position.y-hit.point.y<minClamp)
 57             {
 58                 Vector3 lowPoint = hit.point + new Vector3 (0, minHeight, 0);
 59                 _Camera.position = lowPoint;
 60             }
 61         }
 62     }
 63 
 64     /// <summary>
 65     /// Mouses the scroll wheel.
 66     /// </summary>
 67     private void MouseScrollWheel()
 68     {
 69         if(Input.GetAxis("Mouse ScrollWheel")!=0)
 70         {
 71             _Camera.Translate (new Vector3 (0, 0, Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * scrollSpeed));
 72         }
 73 
 74         CheckLowHeight ();
 75 
 76         if (_Camera.position.y >= maxHeight)
 77             _Camera.position = new Vector3 (_Camera.position.x, maxHeight, _Camera.position.z);
 78     }
 79 
 80     /// <summary>
 81     /// Cameras the move.
 82     /// </summary>
 83     private void CameraMove()
 84     {
 85         if(Input.GetMouseButton(0))
 86         {
 87             if(PreMouseMPos.x<=0)
 88             {
 89                 PreMouseMPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);
 90             }
 91             else
 92             {
 93                 Vector3 CurMouseMPos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 0);
 94                 Vector3 offset = CurMouseMPos - PreMouseMPos;
 95                 offset = -offset * 0.5f * 2;
 96                 if((_Camera.position+offset).y>=minHeight
 97                     &&(_Camera.position+offset).y<=maxHeight)
 98                 {
 99                     _Camera.Translate(offset);
100 
101                     CheckLowHeight();
102 
103                     PreMouseMPos = CurMouseMPos;
104                 }
105             }                
106         }
107         else
108             PreMouseMPos = Vector3.zero;
109     }
110 
111     /// <summary>
112     /// Moves the euler angles.
113     /// </summary>
114     private void MoveEulerAngles()
115     {
116         if(Input.GetMouseButton(1))
117         {
118             float rotationX = _Camera.localEulerAngles.y + Input.GetAxis ("Mouse X") * sensitivityX;
119 
120             rotationY += Input.GetAxis ("Mouse Y") * sensitivityY;
121 
122             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
123 
124             _Camera.localEulerAngles = new Vector3 (-rotationY, rotationX, 0);
125         }
126     }
MoveRocket

第二个是关于摄像机的限制范围和遇到障碍物自动变换与target的距离的功能

代码如下:

  1 public Transform target;
  2 
  3     public LayerMask mask = new LayerMask();
  4 
  5     public Vector2 targetOffset = new Vector2();
  6     public Vector2 originRotation = new Vector2();
  7 
  8     public float distance = 5;
  9     public float minDistance = 0;
 10     public float maxDistance = 10;
 11 
 12     public Vector2 sensitivity = new Vector2(3, 3);
 13 
 14     public float zoomSpeed = 1;
 15     public float zoomSmoothing = 16;
 16 
 17     public float minAngle = -90;
 18     public float maxAngle = 90;
 19 
 20     private float _zoom_in_timer = 0;
 21     private float _zoom_out_timer = 0;
 22 
 23     private float _wanted_distance;
 24     private Quaternion _rotation;
 25     private Vector2 _input_rotation;
 26 
 27     private Transform _t;
 28 
 29     void Start()
 30     {
 31         mask.value = 1;
 32         _t = transform;
 33         _wanted_distance = distance;
 34         _input_rotation = originRotation; 41     }
 42 
 43     void Update()
 44     {
 45         if (target) {
 46 
 47             if (Input.GetMouseButton (0) || Input.GetMouseButton (1)) {
 48                 if (Input.GetAxis ("Mouse X") != 0 || Input.GetAxis ("Mouse Y") != 0) {
 49                     if (!Cursor.visible) {
 50                         Cursor.visible = false;
 51                         Cursor.lockState = CursorLockMode.Locked;
 52                     }
 53                 }
 54 
 55                 return;
 56             }
 57         }
 58 
 59 
 60         if (!Cursor.visible) {
 61             Cursor.visible = true;
 62             Cursor.lockState = CursorLockMode.None;
 63         }
 64 
 65     }
 66     void FixedUpdate()
 67     {
 68         if(target)
 69         {
 70 
 71             // Zoom control
 72             if(Input.GetAxis("Mouse ScrollWheel") < 0 )
 73             {
 74                 _wanted_distance += zoomSpeed;
 75             }
 76             else if(Input.GetAxis("Mouse ScrollWheel") > 0 )
 77             {
 78                 _wanted_distance -= zoomSpeed;
 79             }
 80 
 81 
 82             _wanted_distance = Mathf.Clamp(_wanted_distance, minDistance, maxDistance);
 83 
 84 
 85             if (Input.GetMouseButton (0) || Input.GetMouseButton (1)) {
 86 
 87                 _input_rotation.x += Input.GetAxis ("Mouse X") * sensitivity.x;
 88 
 89 
 90                 ClampRotation ();
 91 
 92 
 93                 _input_rotation.y -= Input.GetAxis ("Mouse Y") * sensitivity.y;
 94 
 95 
 96                 _input_rotation.y = Mathf.Clamp (_input_rotation.y, minAngle, maxAngle);
 97 
 98                 _rotation = Quaternion.Euler (_input_rotation.y, _input_rotation.x, 0);
 99 
100             }
101 
102             // Lerp from current distance to wanted distance
103             distance = Mathf.Clamp(Mathf.Lerp(distance, _wanted_distance, Time.deltaTime * zoomSmoothing), minDistance, maxDistance);
104 
105             // Set wanted position based off rotation and distance
106             Vector3 wanted_position = _rotation * new Vector3(targetOffset.x, 0, -_wanted_distance - 0.2f) + target.position + new Vector3(0, targetOffset.y, 0);
107             Vector3 current_position = _rotation * new Vector3(targetOffset.x, 0, 0) + target.position + new Vector3(0, targetOffset.y, 0);
108 
109 
110             // Linecast to test if there are objects between the camera and the target using collision layers
111             RaycastHit hit;
112 
113             if(Physics.Linecast(current_position, wanted_position, out hit, mask))
114             {
115                 distance = Vector3.Distance(current_position, hit.point) - 0.2f;
116             }
117 
118 
119             // Set the position and rotation of the camera
120             _t.position = _rotation * new Vector3(targetOffset.x, 0.0f, -distance) + target.position + new Vector3(0, targetOffset.y, 0);
121             _t.rotation = _rotation;
122         }
123     }
124 
125     private void ClampRotation()
126     {
127         if(originRotation.x < -180)
128         {
129             originRotation.x += 360;
130         }
131         else if(originRotation.x > 180)
132         {
133             originRotation.x -= 360;
134         }
135 
136         if(_input_rotation.x - originRotation.x < -180)
137         {
138             _input_rotation.x += 360;
139         }
140         else if(_input_rotation.x - originRotation.x > 180)
141         {
142             _input_rotation.x -= 360;
143         }
144     }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:C#汉字转拼音

下一篇:详解ASP.NET MVC Model验证