1. 程式人生 > >模型旋轉縮放

模型旋轉縮放

物體 update input date() void div true gles tran

虛擬仿真實驗常常會用到對模型簡單的旋轉跟縮放。

簡單實現了一下,有加阻尼效果,貼在需要旋轉的模型上便可。

 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(); 29 if (!StringTable.b_isModel) 30 return; 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 }

模型旋轉縮放