欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Unity3D實現(xiàn)物體旋轉縮放移動效果

 更新時間:2019年02月22日 10:22:55   作者:qq_27361571  
這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)物體旋轉縮放移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Unity3D實現(xiàn)物體旋轉縮放移動的具體代碼,供大家參考,具體內容如下

由于項目運行在安卓上,運用到了插件,比較麻煩。你們可以在觸發(fā)條件上進行修改,不用插件也可以。

1.下載FingerGestures 插件 下載地址 點擊打開鏈接

2.導入插件,創(chuàng)建場景 將預設Finger Gestures Initializer 拖拽到 Hierarchy 視圖中

3.添加腳本,拖拽到攝像機上。創(chuàng)建一個方塊拖拽到腳本target 屬性上。

using UnityEngine;
using System.Collections;
 
public class ObjectControl : MonoBehaviour
{
 public Transform target;
 public float yawSensitivity = 80.0f;
 public float pitchSensitivity = 160.0f;
 public bool clampPitchAngle = true;
 public float pinchZoomSensitivity = 0.5f;//縮放速度
 public float smoothZoomSpeed = 10.0f;
 public float smoothOrbitSpeed = 20.0f;
 public float distance = 0;
 
 float yaw = 0;
 float pitch = 0;
 float idealYaw = 0;
 float idealPitch = 0;
 float fChangeScale = 0;
 float fChangeideal = 0;
 public Transform[] movementP;
 
 /// <summary>
 /// 控制模式枚舉
 /// </summary>
 public enum ControlModel
 {
 Zoom, Rotate, Translate
 }
 
 public ControlModel controlModel = ControlModel.Rotate;
 
 //Vector3 position=new Vector3();
 public bool bArrive = false;//鼠標是否到達零件箱邊界區(qū)域
 //平移方式是否根據鼠標拖動距離還是直接置為鼠標位置
 public bool ifDragMove = false;
 //平移方式為:根據鼠標拖動距離 時,評議的速度
 public float moveSpeed = 1.0f;
 //是夠需要畫出按鈕(縮放、旋轉、平移)
 public bool ifDrawBtn = true;
 //縮放方式改為:改變相機范圍
 public bool zoomCamera = false;
 //zoomCamera = true ,相機的最小范圍值
 public float minZoom = 0f;
 //zoomCamera = true ,相機的最大范圍值
 public float maxZoom = 179f;
 //平移對象
 public Transform moveTarget;
 //平移對象的初始位置
 Vector3 moveTargetPos;
 //模型的直接父對象
 public Transform parentModel;
 Vector3 parentModelPos;
 
 void Start()
 {
 zoomCamera = true;
 }
 
 void OnEnable()
 {
 
 FingerGestures.OnDragMove += FingerGestures_OnDragMove;
 FingerGestures.OnPinchMove += FingerGestures_OnPinchMove;
 FingerGestures.OnFingerDragEnd += OnFingerDragEnd;
 
 }
 
 void OnDisable()
 {
 FingerGestures.OnDragMove -= FingerGestures_OnDragMove;
 FingerGestures.OnPinchMove -= FingerGestures_OnPinchMove;
 FingerGestures.OnFingerDragEnd -= OnFingerDragEnd;
 }
 
 public void setRotation()
 {
 Vector3 angles = target.eulerAngles;
 yaw = idealYaw = angles.y;
 pitch = idealPitch = angles.x;
 }
 
 void FingerGestures_OnDragMove(Vector2 fingerPos, Vector2 delta)
 {
 onDrag = true;
 try
 {
  Screen.showCursor = false;
 }
 catch
 {
  Screen.showCursor = false;
 }
 if (controlModel == ControlModel.Rotate && !bArrive)
 {
  idealYaw -= delta.x * yawSensitivity * 0.02f;
  idealPitch += delta.y * pitchSensitivity * 0.02f;
  len = delta;
  if (target) target.transform.Rotate(new Vector3(delta.y, -delta.x, 0), Space.World);
 }
 if (controlModel == ControlModel.Translate && !bArrive)
 {
  if (ifDragMove)
  {
  if (moveTarget == null)
  {
   target.position = new Vector3(target.position.x + delta.x * moveSpeed, target.position.y + delta.y * moveSpeed, target.localPosition.z);// GetWorldPos( fingerPos );
  }
  else
  {
   moveTarget.position = new Vector3(moveTarget.position.x + delta.x * moveSpeed, moveTarget.position.y + delta.y * moveSpeed, moveTarget.localPosition.z);
  }
  }
  else
  {
  if (moveTarget == null)
  {
   target.position = GetWorldPos(fingerPos);
  }
  else
  {
   moveTarget.position = GetWorldPos(fingerPos);
  }
  }
 }
 
 }
 
 void FingerGestures_OnPinchMove(Vector2 fingerPos1, Vector2 fingerPos2, float delta)
 {
 
 if (controlModel == ControlModel.Zoom && !bArrive)
 {
  if (zoomCamera)
  {
  float fZoom = camera.fieldOfView - delta * pinchZoomSensitivity * 800 * Time.deltaTime;
  fZoom = Mathf.Min(fZoom, maxZoom);
  fZoom = Mathf.Max(fZoom, minZoom);
  camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, fZoom, Time.deltaTime * smoothZoomSpeed);
  // camera.transform.position = target.position - fZoom * camera.transform.forward;
  }
  else
  {
 
  fChangeScale = target.localScale.x + delta * pinchZoomSensitivity;
 
  Vector3 vc = new Vector3(fChangeScale, fChangeScale, fChangeScale);
  }
 }
 }
 //滑動結束
 void OnFingerDragEnd(int fingerIndex, Vector2 fingerPos)
 {
 Screen.showCursor = true;
 
 onDrag = false;
 }
 
 
 //把Unity屏幕坐標換算成3D坐標
 Vector3 GetWorldPos(Vector2 screenPos)
 {
 // Camera mainCamera = Camera.main;
 Camera mainCamera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
 if (!mainCamera.enabled)
 {
  mainCamera = mainCamera.transform.parent.FindChild("CameraOne").GetComponent<Camera>();
 }
 return mainCamera.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, Mathf.Abs(target.position.z - mainCamera.transform.position.z)));
 }
 
 void Apply()
 {
 if (controlModel == ControlModel.Rotate && !bArrive)
 {
  yaw = Mathf.Lerp(yaw, idealYaw, Time.deltaTime * smoothOrbitSpeed);
  pitch = Mathf.Lerp(pitch, idealPitch, Time.deltaTime * smoothOrbitSpeed);
 }
 }
 bool onDrag;
 Vector2 len;
 
 void LateUpdate()
 {
 if (Input.GetMouseButtonUp(1) || Input.GetMouseButtonUp(0))
 {
  Screen.showCursor = true;
 }
 Apply();
 }
 
 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);
 }
 
 void Update()
 {
 ///自由切換
 
 if (Input.GetMouseButtonDown(0))
 {
 
  controlModel = ControlModel.Translate;
 }
 
 if (Input.GetMouseButtonDown(1))
 {
 
  controlModel = ControlModel.Rotate;
 }
 
 if (Input.GetAxis("Mouse ScrollWheel") != 0)
 {
  controlModel = ControlModel.Zoom;
 }
 
 }
 
 /// <summary>
 /// 復位
 /// </summary>
 public void ResetValue()
 {
 if (moveTarget != null)
 {
  moveTarget.localPosition = moveTargetPos;
 }
 if (parentModel != null)
 {
  parentModel.localPosition = parentModelPos;
 }
 yaw = 0;
 pitch = 0;
 idealYaw = 0;
 idealPitch = 0;
 }
 
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#創(chuàng)建Windows服務與服務的安裝、卸載

    C#創(chuàng)建Windows服務與服務的安裝、卸載

    這篇文章介紹了C#創(chuàng)建Windows服務與服務的安裝、卸載,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-02-02
  • C#實現(xiàn)的一款比較美觀的驗證碼完整實例

    C#實現(xiàn)的一款比較美觀的驗證碼完整實例

    這篇文章主要介紹了C#實現(xiàn)的一款比較美觀的驗證碼,以完整實例形式分析了C#生成驗證碼與前端調用驗證碼的實現(xiàn)技巧,需要的朋友可以參考下
    2016-06-06
  • C#的Process類調用第三方插件實現(xiàn)PDF文件轉SWF文件

    C#的Process類調用第三方插件實現(xiàn)PDF文件轉SWF文件

    本篇文章主要介紹了C#的Process類調用第三方插件實現(xiàn)PDF文件轉SWF文件,現(xiàn)在分享給大家,具有一定的參考價值,有需要的可以了解一下。
    2016-11-11
  • C#實現(xiàn)類似jQuery的方法連綴功能

    C#實現(xiàn)類似jQuery的方法連綴功能

    這篇文章主要介紹了C#實現(xiàn)類似jQuery的方法連綴功能,可以簡化語句,使代碼變得清晰簡單,感興趣的小伙伴們可以參考一下
    2015-11-11
  • DevExpress之ChartControl實現(xiàn)柱狀圖演示實例

    DevExpress之ChartControl實現(xiàn)柱狀圖演示實例

    這篇文章主要介紹了DevExpress中ChartControl實現(xiàn)柱狀圖演示方法,實例展示了相關繪圖函數的具體用法,具有一定的實用價值,需要的朋友可以參考下
    2014-10-10
  • C#子線程執(zhí)行完后通知主線程的方法

    C#子線程執(zhí)行完后通知主線程的方法

    下面小編就為大家?guī)硪黄狢#子線程執(zhí)行完后通知主線程的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-12-12
  • C#中使用ArrayPool和MemoryPool實例

    C#中使用ArrayPool和MemoryPool實例

    對資源的可復用是提升應用程序性能的一個非常重要的手段,比如本篇要分享的 ArrayPool 和 MemoryPool,它們就有效的減少了內存使用和對GC的壓力,從而提升應用程序性能。感興趣的可以了解一下
    2021-05-05
  • C# wpf解決Popup彈出位置異常問題解決

    C# wpf解決Popup彈出位置異常問題解決

    本文主要介紹了C# wpf解決Popup彈出位置異常問題解決,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-10-10
  • C# 實現(xiàn)Zookeeper分布式鎖的參考示例

    C# 實現(xiàn)Zookeeper分布式鎖的參考示例

    Zookeeper分布式鎖的原理是巧妙的是使用了znode臨時節(jié)點的特點和監(jiān)聽(watcher)機制,監(jiān)聽機制很簡單,就是我們可以給znode添加一個監(jiān)聽器,當znode節(jié)點狀態(tài)發(fā)生改變時(如:數據內容改變,節(jié)點被刪除),會通知到監(jiān)聽器。本文講解使用c#實現(xiàn)該功能
    2021-06-06
  • .net的命名空間類庫的簡單介紹

    .net的命名空間類庫的簡單介紹

    .net的命名空間類庫的簡單介紹,需要的朋友可以參考一下
    2013-04-04

最新評論