Unity3D 拖动物体旋转和缩放

[csharp]  view plain  copy
 
  在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class ThisRotate : MonoBehaviour  
  5. {  
  6.     Vector3 StartPosition;  
  7.     Vector3 previousPosition;  
  8.     Vector3 offset;  
  9.     Vector3 finalOffset;  
  10.     Vector3 eulerAngle;  
  11.   
  12.     bool isSlide;  
  13.     float angle;  
  14.   
  15.     public float scale = 1;  
  16.   
  17.     RaycastHit hit;  
  18.   
  19.     void Start()  
  20.     {  
  21.   
  22.     }  
  23.   
  24.     void OnGUI()  
  25.     {  
  26.           
  27.         Ray ray =Camera.mainCamera.ScreenPointToRay(Input.mousePosition);  
  28.         if(Physics.Raycast(ray, out hit))  
  29.         {  
  30.             if (hit.transform == transform)  
  31.             {  
  32.                 if (Input.GetAxis("Mouse ScrollWheel") != 0)  
  33.                 {  
  34.                     Debug.Log(Input.GetAxis("Mouse ScrollWheel"));  
  35.                     scale = scale + Input.GetAxis("Mouse ScrollWheel");  
  36.                     transform.localScale = new Vector3(1 + scale, 1 + scale, 1 + scale);  
  37.                 }  
  38.             }  
  39.         }  
  40.     }  
  41.   
  42.     void Update()  
  43.     {  
  44.         if (Input.GetMouseButtonDown(0))  
  45.         {  
  46.             StartPosition = Input.mousePosition;  
  47.             previousPosition = Input.mousePosition;  
  48.         }  
  49.         if (Input.GetMouseButton(0))  
  50.         {  
  51.             offset = Input.mousePosition - previousPosition;  
  52.             previousPosition = Input.mousePosition;  
  53.             transform.Rotate(Vector3.Cross(offset, Vector3.forward).normalized, offset.magnitude, Space.World);  
  54.   
  55.         }  
  56.         if (Input.GetMouseButtonUp(0))  
  57.         {  
  58.             finalOffset = Input.mousePosition - StartPosition;  
  59.             isSlide = true;  
  60.             angle = finalOffset.magnitude;  
  61.   
  62.   
  63.         }  
  64.         if (isSlide)  
  65.         {  
  66.             transform.Rotate(Vector3.Cross(finalOffset, Vector3.forward).normalized, angle * 2 * Time.deltaTime, Space.World);  
  67.             if (angle > 0)  
  68.             {  
  69.                 angle -= 5;  
  70.             }  
  71.             else  
  72.             {  
  73.                 angle = 0;  
  74.             }  
  75.         }  
  76.     }  
  77. }  

猜你喜欢

转载自hypercube.iteye.com/blog/2319148