欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

unity实现车方向盘转动效果

程序员文章站 2023-02-19 15:47:42
本文实例为大家分享了unity实现车方向盘转动效果的具体代码,供大家参考,具体内容如下效果:c#脚本如下:using system;using system.collections;using sys...

本文实例为大家分享了unity实现车方向盘转动效果的具体代码,供大家参考,具体内容如下

效果:

unity实现车方向盘转动效果

c#脚本如下:

using system;
using system.collections;
using system.collections.generic;
using unityengine;
using unityengine.eventsystems;

public class newbehaviourscript : monobehaviour, idraghandler,ibegindraghandler,ienddraghandler
{
  /// <summary>
  /// 需要旋转的模型
  /// </summary>
  public transform model;
  /// <summary>
  /// 父物体的recttransform
  /// </summary>
  public recttransform parentrect;
  /// <summary>
  /// 旋转的ui
  /// </summary>
  public recttransform img;
  /// <summary>
  /// 摄像机
  /// </summary>
  public camera cam;
  /// <summary>
  /// 是否在拖拽
  /// </summary>
  private bool drag = false;
  /// <summary>
  /// 初始角度
  /// </summary>
  private float originangle = 0;
  /// <summary>
  /// 自身角度
  /// </summary>
  private float selfangle1 = 0;
  /// <summary>
  /// 上一次和当前的角度
  /// </summary>
  private float lastangle = 0f;
  private float currentangle = 0f;
  /// <summary>
  /// 上一次和当前的位置
  /// </summary>
  private vector2 currentpos;
  private vector2 lastpos;

  public void onbegindrag(pointereventdata eventdata)
  {
    drag = true;

    originangle = getangle(eventdata.position);

    selfangle1 = (img.transform as recttransform).eulerangles.z;
  }

  public void ondrag(pointereventdata eventdata)
  {
    if (drag)
    {
      lastangle = currentangle;
      currentangle = getangle(eventdata.position);

      float val = touchjudge(currentpos, ref lastpos, vector2.zero);
      if (val > 0f && val <180f)
      {
        img.eulerangles = new vector3(0f,0f, -currentangle + originangle + selfangle1);
        model.eulerangles = new vector3(0f, 0f, -currentangle + originangle + selfangle1);
      }
    }
  }

  public void onenddrag(pointereventdata eventdata)
  {
    drag = false;
  }
  /// <summary>
  /// 将屏幕坐标转成ui坐标
  /// </summary>
  /// <param name="pos1"></param>
  /// <returns></returns>
  float getangle(vector2 pos1) {
    vector2 pos;
    recttransformutility.screenpointtolocalpointinrectangle(parentrect, pos1, cam, out pos);
    currentpos = pos;
    return mathf.atan2(pos.x, pos.y) * mathf.rad2deg;
  }
  /// <summary>
  /// 判断顺时针还是逆时针旋转
  /// </summary>
  /// <param name="current"></param>
  /// <param name="last"></param>
  /// <param name="anchor"></param>
  /// <returns></returns>
  private float touchjudge(vector2 current, ref vector2 last, vector2 anchor)
  {
    vector2 lastdir = (last - anchor).normalized; 
    vector2 currentdir = (current - anchor).normalized;

    float lastdot = vector2.dot(vector2.right, lastdir);
    float currentdot = vector2.dot(vector2.right, currentdir);

    float lastangle = last.y < anchor.y
       ? mathf.acos(lastdot) * mathf.rad2deg
       : -mathf.acos(lastdot) * mathf.rad2deg;

    float currentangle = current.y < anchor.y
      ? mathf.acos(currentdot) * mathf.rad2deg
      : -mathf.acos(currentdot) * mathf.rad2deg;

    last = current;
    return currentangle - lastangle;
  }
}

canvas设置如下:

unity实现车方向盘转动效果

脚本赋值如下:

unity实现车方向盘转动效果

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。