Unity3D實(shí)現(xiàn)物體旋轉(zhuǎn)縮放移動(dòng)效果
本文實(shí)例為大家分享了Unity3D實(shí)現(xiàn)物體旋轉(zhuǎn)縮放移動(dòng)的具體代碼,供大家參考,具體內(nèi)容如下
由于項(xiàng)目運(yùn)行在安卓上,運(yùn)用到了插件,比較麻煩。你們可以在觸發(fā)條件上進(jìn)行修改,不用插件也可以。
1.下載FingerGestures 插件 下載地址 點(diǎn)擊打開鏈接
2.導(dǎo)入插件,創(chuàng)建場(chǎng)景 將預(yù)設(shè)Finger Gestures Initializer 拖拽到 Hierarchy 視圖中
3.添加腳本,拖拽到攝像機(jī)上。創(chuàng)建一個(gè)方塊拖拽到腳本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;//鼠標(biāo)是否到達(dá)零件箱邊界區(qū)域
//平移方式是否根據(jù)鼠標(biāo)拖動(dòng)距離還是直接置為鼠標(biāo)位置
public bool ifDragMove = false;
//平移方式為:根據(jù)鼠標(biāo)拖動(dòng)距離 時(shí),評(píng)議的速度
public float moveSpeed = 1.0f;
//是夠需要畫出按鈕(縮放、旋轉(zhuǎn)、平移)
public bool ifDrawBtn = true;
//縮放方式改為:改變相機(jī)范圍
public bool zoomCamera = false;
//zoomCamera = true ,相機(jī)的最小范圍值
public float minZoom = 0f;
//zoomCamera = true ,相機(jī)的最大范圍值
public float maxZoom = 179f;
//平移對(duì)象
public Transform moveTarget;
//平移對(duì)象的初始位置
Vector3 moveTargetPos;
//模型的直接父對(duì)象
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);
}
}
}
//滑動(dòng)結(jié)束
void OnFingerDragEnd(int fingerIndex, Vector2 fingerPos)
{
Screen.showCursor = true;
onDrag = false;
}
//把Unity屏幕坐標(biāo)換算成3D坐標(biāo)
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;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:Unity3D利用DoTween實(shí)現(xiàn)卡牌翻轉(zhuǎn)效果
本文標(biāo)題:Unity3D實(shí)現(xiàn)物體旋轉(zhuǎn)縮放移動(dòng)效果
本文地址:http://www.jygsgssxh.com/a1/C_jiaocheng/4873.html
您可能感興趣的文章
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新聞效果的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已安裝軟件變化的方法
- 01-10C#實(shí)現(xiàn)多線程下載文件的方法
- 01-10C#實(shí)現(xiàn)Winform中打開網(wǎng)頁(yè)頁(yè)面的方法
- 01-10C#實(shí)現(xiàn)遠(yuǎn)程關(guān)閉計(jì)算機(jī)或重啟計(jì)算機(jī)的方法
- 01-10C#自定義簽名章實(shí)現(xiàn)方法
- 01-10C#文件斷點(diǎn)續(xù)傳實(shí)現(xiàn)方法
- 01-10winform實(shí)現(xiàn)創(chuàng)建最前端窗體的方法


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10delphi制作wav文件的方法
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子


