Unity C # Some experiences with Camera !,
This article is original, reproduced please indicate the source: http://www.cnblogs.com/AdvancePikachu/p/6856374.html
First, I summarized the camera roaming functions in my recent work,
The script is as follows:
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 // up/down Max Y angle 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 _ C Amera. 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 else108 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
The second is about the camera's restricted range and the function of automatically changing the distance from the target when encountering obstacles.
The Code is as follows:
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 distance103 distance = Mathf.Clamp(Mathf.Lerp(distance, _wanted_distance, Time.deltaTime * zoomSmoothing), minDistance, maxDistance);104 105 // Set wanted position based off rotation and distance106 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 layers111 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 camera120 _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 }