1. 程式人生 > >總結聖典中操作物體任意方向旋轉的三種方法

總結聖典中操作物體任意方向旋轉的三種方法

方法一:
複製程式碼
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 private Transform hitTransfrom; void Update() { if (Input.GetMouseButtonDown(0)) { RaycastHit hit;
Ray mouseray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(mouseray, out hit)) { hitTransfrom = hit.transform; } } else if (Input.GetMouseButtonUp(0)) { hitTransfrom = null; } if (hitTransfrom) { Matrix4x4 localmatrix = hitTransfrom.worldToLocalMatrix;
Vector3 vUp = localmatrix.MultiplyVector(new Vector3(0, 1, 0)); Vector3 vRight = -localmatrix.MultiplyVector(new Vector3(1, 0, 0)); float fMoveX = -Input.GetAxis("Mouse X") * Time.deltaTime * 200.0f; Quaternion rotation = Quaternion.AngleAxis(fMoveX, vUp); hitTransfrom.localRotation *= rotation;
float fMoveY = -Input.GetAxis("Mouse Y") * Time.deltaTime * 200.0f; Quaternion rotoy = Quaternion.AngleAxis(fMoveY, vRight); hitTransfrom.localRotation *= rotoy; } }
方法二: 

複製程式碼