The unity mobile game realizes the steering wheel effect (rotation, rotation, angle limit)

Effect
Drag the steering wheel with your finger or mouse to rotate, there is an angle limit, and it will automatically rotate after letting go.
insert image description here
Code
Add code to the steering wheel Image.
Note that Canvas needs to be assigned.

using UnityEngine;
using UnityEngine.EventSystems;

public class SteeringWheel : MonoBehaviour,IDragHandler,IEndDragHandler,IBeginDragHandler
{
    
    
    public Canvas CanvasRoot;//需要指定画布 

    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(CanvasRoot.GetComponent<RectTransform>(),
#if !UNITY_EDITOR
                Input.GetTouch(0).position,
#else
                Input.mousePosition,
#endif
            CanvasRoot.worldCamera, out pos))    //获取鼠标点击位置
        {
    
    
            Vector3 pos3 = new Vector3(pos.x, pos.y, 0);                           //计算后鼠标以方向盘圆心为坐标原点的坐标位置
            pos3 = CanvasRoot.transform.TransformPoint(pos3); //转换为世界坐标

            if (m_IsFirst)
            {
    
    
                m_CurrentPos = pos3;
                m_IsFirst = false;
            }
            Vector3 currentPos = Vector3.Cross(pos3 - transform.position, m_CurrentPos - transform.position);             //计算当前帧和上一帧手指位置 用于判断旋转方向
            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 <= MAX_ANGLE)
                    {
    
    
                        m_RoundValue += Vector3.Angle(m_CurrentPos - transform.position, pos3 - transform.position) * speedMul;

                        transform.Rotate(new Vector3(0, 0, -Vector3.Angle(m_CurrentPos - transform.position, pos3 - transform.position)) * speedMul);
                    }
                }

                else
                {
    
    
                    if (m_RoundValue >= -MAX_ANGLE)
                    {
    
    
                        m_RoundValue -= Vector3.Angle(m_CurrentPos - transform.position, pos3 - transform.position) * speedMul;

                        transform.Rotate(new Vector3(0, 0, Vector3.Angle(m_CurrentPos - transform.position, pos3 - transform.position)) * speedMul);
                    }
                }

            }
            m_CurrentPos = pos3;
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
    
    
        m_IsFirst = true;
        m_IsTuringSteeringWheel = false;
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
    
    
        m_IsTuringSteeringWheel = true;
    }

    void Start()
    {
    
    
        CanvasRoot = GameObject.Find("Canvas").GetComponent<Canvas>();
    }


    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));
            }
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_39162826/article/details/120139695