unity實現方向盤轉動效果

本文實例為大傢分享瞭unity實現方向盤轉動效果的具體代碼,供大傢參考,具體內容如下

效果

手指或鼠標拖動方向盤旋轉,有角度限制,松手後自動回轉。

代碼

將代碼添加到方向盤Image上。

註意需要賦值Canvas。

using UnityEngine;
using UnityEngine.EventSystems;

public class SteeringWheel : MonoBehaviour,IDragHandler,IEndDragHandler
{
    public Canvas CanvasRoot;//需要指定畫佈 
    private RectTransform m_RectTransform;//坐標

    private bool m_IsFirst = true;           //用於記錄第一幀按下鼠標時鼠標的位置,便於計算
    private Vector3 m_CurrentPos;            //記錄當前幀鼠標所在位置
    private bool m_IsClockwise;              //是否順時針
    private float m_RoundValue = 0;          //記錄總的旋轉角度 用這個數值來控制一圈半
    private bool m_IsTuringSteeringWheel;    //是否在轉方向盤 用這個判斷復位

    public void OnDrag(PointerEventData eventData)
    {
        m_IsTuringSteeringWheel = true;
        Vector2 pos;
        if (RectTransformUtility.ScreenPointToLocalPointInRectangle(m_RectTransform, Input.mousePosition, CanvasRoot.worldCamera, out pos))    //獲取鼠標點擊位置
        {

            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);   //計算後鼠標以方向盤圓心為坐標原點的坐標位置

            if (m_IsFirst)
            {
                m_CurrentPos = pos3;
                m_IsFirst = false;
            }

            Vector3 currentPos = Vector3.Cross(pos3, m_CurrentPos);      //計算當前幀和上一幀手指位置 用於判斷旋轉方向
            if (currentPos.z > 0)
            {
                m_IsClockwise = true;
            }
            else if (currentPos.z < 0)
            {
                m_IsClockwise = false;
            }

            if (m_CurrentPos != pos3)     //范圍內讓方向盤隨著手指轉動
            {
                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)   //復位
        {
            if (m_RoundValue >= 0)
            {
                m_RoundValue -= 8f;               //復位速度
                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));
            }
        }
    }
}

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

推薦閱讀: