1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| using UnityEngine; using System.Collections;
public class MouseMoveCube : MonoBehaviour {
private void Start() { StartCoroutine(OnMouseDown()); }
private void Update() { ZoomCube(); }
private void ZoomCube() { //projection:投射方式
//zoom out if (Input.GetAxis("Mouse ScrollWheel") < 0) { //Perspective:透视 if (Camera.main.fieldOfView <= 80) { Camera.main.fieldOfView += 2; }
//Orthographic:正交 if (Camera.main.orthographicSize <= 18) { Camera.main.orthographicSize += 0.5f; } }
//zoom in if (Input.GetAxis("Mouse ScrollWheel") > 0) {
if (Camera.main.fieldOfView >= 2) { Camera.main.fieldOfView -= 2; }
if (Camera.main.orthographicSize >= 1) { Camera.main.orthographicSize -= 0.5f; } } }
IEnumerator OnMouseDown() {
//将物体由世界坐标系转换为屏幕坐标系 Vector3 screenPostion = Camera.main.WorldToScreenPoint(transform.position);
//将鼠标屏幕坐标转为三维坐标,再算出物体位置与鼠标之间的距离(offset) Vector3 offset = transform.position-Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPostion.z));
while( Input.GetMouseButton(0) ) { //当前鼠标位置 Vector3 curMousePostion = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPostion.z);
//将鼠标位置转换为世界位置,加上偏移量 Vector3 curPosition = Camera.main.ScreenToWorldPoint(curMousePostion) + offset;
//将当前位置赋给世界物体的位置 transform.position = curPosition;
yield return new WaitForFixedUpdate();//循环 } } }
|