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

unity實(shí)現(xiàn)方向盤轉(zhuǎn)動(dòng)效果

 更新時(shí)間:2021年09月07日 16:00:35   作者:我寄人間雪滿頭丶  
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)方向盤轉(zhuǎn)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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));
            }
        }
    }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論