unity實(shí)現(xiàn)方向盤轉(zhuǎn)動(dòng)效果
本文實(shí)例為大家分享了unity實(shí)現(xiàn)方向盤轉(zhuǎn)動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下
效果
手指或鼠標(biāo)拖動(dòng)方向盤旋轉(zhuǎn),有角度限制,松手后自動(dòng)回轉(zhuǎn)。

代碼
將代碼添加到方向盤Image上。
注意需要賦值Canvas。
using UnityEngine;
using UnityEngine.EventSystems;
public class SteeringWheel : MonoBehaviour,IDragHandler,IEndDragHandler
{
public Canvas CanvasRoot;//需要指定畫布
private RectTransform m_RectTransform;//坐標(biāo)
private bool m_IsFirst = true; //用于記錄第一幀按下鼠標(biāo)時(shí)鼠標(biāo)的位置,便于計(jì)算
private Vector3 m_CurrentPos; //記錄當(dāng)前幀鼠標(biāo)所在位置
private bool m_IsClockwise; //是否順時(shí)針
private float m_RoundValue = 0; //記錄總的旋轉(zhuǎn)角度 用這個(gè)數(shù)值來控制一圈半
private bool m_IsTuringSteeringWheel; //是否在轉(zhuǎn)方向盤 用這個(gè)判斷復(fù)位
public void OnDrag(PointerEventData eventData)
{
m_IsTuringSteeringWheel = true;
Vector2 pos;
if (RectTransformUtility.ScreenPointToLocalPointInRectangle(m_RectTransform, Input.mousePosition, CanvasRoot.worldCamera, out pos)) //獲取鼠標(biāo)點(diǎn)擊位置
{
pos.x = pos.x + (Screen.width / 2) - GetComponent<RectTransform>().position.x;
pos.y = pos.y + (Screen.height / 2) - GetComponent<RectTransform>().position.y;
Vector3 pos3 = new Vector3(pos.x, pos.y, 0); //計(jì)算后鼠標(biāo)以方向盤圓心為坐標(biāo)原點(diǎn)的坐標(biāo)位置
if (m_IsFirst)
{
m_CurrentPos = pos3;
m_IsFirst = false;
}
Vector3 currentPos = Vector3.Cross(pos3, m_CurrentPos); //計(jì)算當(dāng)前幀和上一幀手指位置 用于判斷旋轉(zhuǎn)方向
if (currentPos.z > 0)
{
m_IsClockwise = true;
}
else if (currentPos.z < 0)
{
m_IsClockwise = false;
}
if (m_CurrentPos != pos3) //范圍內(nèi)讓方向盤隨著手指轉(zhuǎn)動(dòng)
{
if (m_IsClockwise)
{
if (m_RoundValue <= 180)
{
m_RoundValue += Vector3.Angle(m_CurrentPos, pos3);
transform.Rotate(new Vector3(0, 0, -Vector3.Angle(m_CurrentPos, pos3)));
}
}
else
{
if (m_RoundValue >= -180)
{
m_RoundValue -= Vector3.Angle(m_CurrentPos, pos3);
transform.Rotate(new Vector3(0, 0, Vector3.Angle(m_CurrentPos, pos3)));
}
}
}
m_CurrentPos = pos3;
}
}
public void OnEndDrag(PointerEventData eventData)
{
m_IsFirst = true;
m_IsTuringSteeringWheel = false;
}
void Start()
{
CanvasRoot = GameObject.Find("Canvas").GetComponent<Canvas>();
m_RectTransform = CanvasRoot.transform as RectTransform;
}
void Update()
{
if (!m_IsTuringSteeringWheel && m_RoundValue != 0) //復(fù)位
{
if (m_RoundValue >= 0)
{
m_RoundValue -= 8f; //復(fù)位速度
if (m_RoundValue < 0)
m_RoundValue = 0;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, -m_RoundValue));
}
else
{
m_RoundValue += 8f;
if (m_RoundValue > 0)
m_RoundValue = 0;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, -m_RoundValue));
}
}
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
給c#添加SetTimeout和SetInterval函數(shù)
Javascript中的SetTimeout和SetInterval函數(shù)很方便,把他們移植到c#中來。2008-03-03
C#實(shí)現(xiàn)在控制臺(tái)輸入密碼顯示星號(hào)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)在控制臺(tái)輸入密碼顯示星號(hào)的方法,感興趣的小伙伴們可以參考一下2016-04-04
C#實(shí)現(xiàn)實(shí)時(shí)監(jiān)控文件夾變化
在開發(fā)各種應(yīng)用程序時(shí),我們經(jīng)常需要對(duì)文件系統(tǒng)中的文件或文件夾進(jìn)行實(shí)時(shí)監(jiān)測,下面就跟隨小編一起來看看具體如何使用C#實(shí)現(xiàn)這一功能吧2024-03-03
LINQ基礎(chǔ)之Intersect、Except和Distinct子句
這篇文章介紹了LINQ使用Intersect、Except和Distinct子句的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04

