Unity3D實現(xiàn)物體旋轉(zhuǎn)縮放移動效果-創(chuàng)新互聯(lián)

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

創(chuàng)新互聯(lián)公司公司2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項目網(wǎng)站設(shè)計制作、成都做網(wǎng)站網(wǎng)站策劃,項目實施與項目整合能力。我們以讓每一個夢想脫穎而出為使命,1280元鎮(zhèn)雄做網(wǎng)站,已為上家服務(wù),為鎮(zhèn)雄各地企業(yè)和個人服務(wù),聯(lián)系電話:18980820575

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

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

2.導(dǎo)入插件,創(chuàng)建場景 將預(yù)設(shè)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ū)域
 //平移方式是否根據(jù)鼠標拖動距離還是直接置為鼠標位置
 public bool ifDragMove = false;
 //平移方式為:根據(jù)鼠標拖動距離 時,評議的速度
 public float moveSpeed = 1.0f;
 //是夠需要畫出按鈕(縮放、旋轉(zhuǎn)、平移)
 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);
  }
 }
 }
 //滑動結(jié)束
 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>
 /// 復(fù)位
 /// </summary>
 public void ResetValue()
 {
 if (moveTarget != null)
 {
  moveTarget.localPosition = moveTargetPos;
 }
 if (parentModel != null)
 {
  parentModel.localPosition = parentModelPos;
 }
 yaw = 0;
 pitch = 0;
 idealYaw = 0;
 idealPitch = 0;
 }
 
}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)站欄目:Unity3D實現(xiàn)物體旋轉(zhuǎn)縮放移動效果-創(chuàng)新互聯(lián)
本文網(wǎng)址:http://bm7419.com/article46/hcchg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計、域名注冊、網(wǎng)站導(dǎo)航ChatGPT、網(wǎng)站營銷、商城網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)