模型旋转缩放
虚拟仿真实验常常会用到对模型简单的旋转跟缩放。
简单实现了一下,有加阻尼效果,贴在需要旋转的模型上便可。
1 using UnityEngine; 2 using System.Collections; 3 4 public class modelController : MonoBehaviour { 5 private float axisX; 6 private float axisY; 7 8 private float leap_Distance; 9 bool iszuni; 10 bool isxuanzhuan; 11 12 float x; 13 float y; 14 15 float f_beginSize; 16 Vector3 v_beginRotate; 17 18 void Awake() 19 { 20 axisY = 0; 21 axisX = 0; 22 leap_Distance = f_beginSize = transform.localScale.x; 23 v_beginRotate = transform.localEulerAngles; 24 } 25 26 void Update() 27 { 28 ZoomScale();
31 if (iszuni) 32 { 33 transform.localScale = Vector3.Lerp(transform.localScale, Vector3.one * leap_Distance, 0.2f); 34 } 35 36 if (Input.GetMouseButton(0)) 37 {//鼠标按着左键移动 38 y = Input.GetAxis("Mouse X") * 8; 39 x = Input.GetAxis("Mouse Y") * 8; 40 isxuanzhuan = true; 41 } 42 else 43 { 44 x = y = 0; 45 } 46 if (isxuanzhuan) 47 { 48 axisY = Mathf.Lerp(axisY, y,0.2f); 49 axisX = Mathf.Lerp(axisX, x,0.2f); 50 //旋转角度(增加) 51 transform.Rotate(new Vector3(axisX, -axisY, 0), Space.World); 52 } 53 } 54 55 56 /// <summary> 57 /// 鼠标滚轮缩放物体的方法(Scale) 58 /// </summary> 59 void ZoomScale() 60 { 61 if (Input.GetAxis("Mouse ScrollWheel") != 0) 62 { 63 leap_Distance += Input.GetAxis("Mouse ScrollWheel") * f_beginSize; 64 //最大扩大2倍,最小缩小0.5倍 65 if (leap_Distance < (f_beginSize * 0.5f)) 66 { 67 leap_Distance = f_beginSize * 0.5f; 68 } 69 if (leap_Distance > f_beginSize * 2) 70 { 71 leap_Distance = f_beginSize * 2; 72 } 73 iszuni = true; 74 } 75 } 76 77 /// <summary> 78 /// 重置 79 /// </summary> 80 public void ResetScale() 81 { 82 iszuni = false; 83 leap_Distance = f_beginSize; 84 transform.localScale = Vector3.one * f_beginSize; 85 transform.localEulerAngles = v_beginRotate; 86 } 87 }