unity3d鼠标拖拽模型,旋转模型
生活随笔
收集整理的這篇文章主要介紹了
unity3d鼠标拖拽模型,旋转模型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
直接掛載到模型上
using UnityEngine; using System.Collections;public class OnDrag : MonoBehaviour {//目標物體public Transform target;private int MouseWheelSensitivity = 1; //放大倍數的快慢private int MouseZoomMin = 1; //最小倍數private int MouseZoomMax = 5; //最大倍數//默認距離private float normalDistance = 3;private Vector3 normalized;//拖拽的移動速度private float xSpeed = 250.0f;private float ySpeed = 120.0f;//拖拽的高度限制private int yMinLimit = -20;private int yMaxLimit = 80;//角度private float x = 0.0f;private float y = 0.0f;//記錄目標物體的坐標private Vector3 screenPoint;private Vector3 offset;//圍繞x旋轉30°private Quaternion rotation = Quaternion.Euler(new Vector3(30f, 0f, 0f));//目標的3D坐標private Vector3 CameraTarget;//打印歐拉角:繞各個軸旋轉的角度,順時針為正方向public void Awake(){target = transform;print(transform.eulerAngles.x);print(transform.eulerAngles.y);print(transform.eulerAngles.z);}void Start(){//找到目標飛機的3d坐標CameraTarget = target.position;//目標飛機的z-3,距離攝像機更近了float z = target.transform.position.z - normalDistance;//給當前相機給定位,現在的3D坐標乘以30°transform.position = rotation * new Vector3(transform.position.x, transform.position.y, z);//將視角轉向物體transform.LookAt(target);//記錄各個軸偏離的角度var angles = transform.eulerAngles;x = angles.y;y = angles.x;}void OnGUI(){GUI.Label(new Rect(10, 10, 300, 30), "左擊:旋轉;滾輪:縮放;中擊:拖拽");}void Update(){//如果左擊了,旋轉if (Input.GetMouseButton(0)){x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;y = ClampAngle(y, yMinLimit, yMaxLimit);var rotation = Quaternion.Euler(y, x, 0);var position = rotation * new Vector3(0.0f, 0.0f, -normalDistance) + CameraTarget;transform.rotation = rotation;transform.position = position;}//滾輪縮放else if (Input.GetAxis("Mouse ScrollWheel") != 0){//攝像機3d坐標-物體的3d坐標normalized = (transform.position - CameraTarget).normalized;if (normalDistance >= MouseZoomMin && normalDistance <= MouseZoomMax){normalDistance -= Input.GetAxis("Mouse ScrollWheel") * MouseWheelSensitivity;}if (normalDistance < MouseZoomMin){normalDistance = MouseZoomMin;}if (normalDistance > MouseZoomMax){normalDistance = MouseZoomMax;}//改變攝像機的遠近transform.position = normalized * normalDistance;}//案件按下 記錄鼠標的else if (Input.GetMouseButtonDown(2)){//將目標物體的坐標轉化成平面坐標screenPoint = Camera.main.WorldToScreenPoint(target.transform.position);//計算鼠標的3維坐標跟物體的3維坐標的差值offset = target.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));}//中鍵拖拽,改變飛機的坐標,每幀調用if (Input.GetMouseButton(2)){//鼠標的平面坐標Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);//鼠標轉移的3d空間坐標值Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;//改變鼠標的3D空間坐標target.transform.position = curPosition;}//朝向,每動一幀都要改變朝向transform.LookAt(CameraTarget);}//控制旋轉的角度,如果旋轉的角度大于360或者小于360都要加上或者減去對應的角度static float ClampAngle(float angle, float min, float max){if (angle < -360)angle += 360;if (angle > 360)angle -= 360;return Mathf.Clamp(angle, min, max);} }總結
以上是生活随笔為你收集整理的unity3d鼠标拖拽模型,旋转模型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【J2EE之web应用】java集群概念
- 下一篇: 读《Oracle DBA工作笔记》知识点