unity方向盘转动效果 unity实现方向盘转动效果

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

unity方向盘转动效果 unity实现方向盘转动效果

我寄人间雪满头丶   2021-09-07 我要评论
想了解unity实现方向盘转动效果的相关内容吗,我寄人间雪满头丶在本文为您仔细讲解unity方向盘转动效果的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:unity方向盘转动效果,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));
            }
        }
    }
}

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们