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

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

 更新時(shí)間:2020年04月18日 12:38:24   作者:貪玩的孩紙時(shí)代  
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)車方向盤轉(zhuǎn)動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了unity實(shí)現(xiàn)車方向盤轉(zhuǎn)動(dòng)效果的具體代碼,供大家參考,具體內(nèi)容如下

效果:

C#腳本如下:

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class NewBehaviourScript : MonoBehaviour, IDragHandler,IBeginDragHandler,IEndDragHandler
{
  /// <summary>
  /// 需要旋轉(zhuǎn)的模型
  /// </summary>
  public Transform model;
  /// <summary>
  /// 父物體的recttransform
  /// </summary>
  public RectTransform parentRect;
  /// <summary>
  /// 旋轉(zhuǎn)的UI
  /// </summary>
  public RectTransform img;
  /// <summary>
  /// 攝像機(jī)
  /// </summary>
  public Camera cam;
  /// <summary>
  /// 是否在拖拽
  /// </summary>
  private bool drag = false;
  /// <summary>
  /// 初始角度
  /// </summary>
  private float originAngle = 0;
  /// <summary>
  /// 自身角度
  /// </summary>
  private float selfAngle1 = 0;
  /// <summary>
  /// 上一次和當(dāng)前的角度
  /// </summary>
  private float lastAngle = 0f;
  private float currentAngle = 0f;
  /// <summary>
  /// 上一次和當(dāng)前的位置
  /// </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>
  /// 將屏幕坐標(biāo)轉(zhuǎn)成UI坐標(biāo)
  /// </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>
  /// 判斷順時(shí)針還是逆時(shí)針旋轉(zhuǎn)
  /// </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設(shè)置如下:

腳本賦值如下:

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

相關(guān)文章

  • C# CSV文件讀寫的實(shí)現(xiàn)

    C# CSV文件讀寫的實(shí)現(xiàn)

    本文主要介紹了C# CSV文件讀寫的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • C#使用Object類實(shí)現(xiàn)棧的方法詳解

    C#使用Object類實(shí)現(xiàn)棧的方法詳解

    這篇文章主要介紹了C#使用Object類實(shí)現(xiàn)棧的方法,詳細(xì)分析了棧的原理及使用Object類實(shí)現(xiàn)棧的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下
    2016-06-06
  • C#創(chuàng)建線程帶參數(shù)的方法

    C#創(chuàng)建線程帶參數(shù)的方法

    本文給大家介紹C#創(chuàng)建線程帶參數(shù)的方法,包括無(wú)參數(shù)線程的創(chuàng)建,帶一個(gè)參數(shù)線程的創(chuàng)建及帶兩個(gè)及以上參數(shù)線程的創(chuàng)建,非常不錯(cuò),具有參考借鑒價(jià)值,感興趣的朋友一起看下吧
    2016-07-07
  • C#異步的世界(下)

    C#異步的世界(下)

    這篇文章主要介紹了C#異步的世界(下),對(duì)異步感興趣的同學(xué),可以參考下
    2021-04-04
  • 在Framework 4.0中:找出新增的方法與新增的類(二)

    在Framework 4.0中:找出新增的方法與新增的類(二)

    為什么動(dòng)態(tài)加載程序集無(wú)法找出Framework 4.0 和Framwork2.0 新增的方法和類
    2013-05-05
  • c# 將Datatable數(shù)據(jù)導(dǎo)出到Excel表格中

    c# 將Datatable數(shù)據(jù)導(dǎo)出到Excel表格中

    本文主要介紹了c# 將Datatable數(shù)據(jù)導(dǎo)出到Excel表格中的方法。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧
    2017-03-03
  • C#使用GDI畫圓的方法

    C#使用GDI畫圓的方法

    這篇文章主要介紹了C#使用GDI畫圓的方法,涉及C#使用GDI繪圖的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-04-04
  • c# WPF實(shí)現(xiàn)Windows資源管理器(附源碼)

    c# WPF實(shí)現(xiàn)Windows資源管理器(附源碼)

    這篇文章主要介紹了c# WPF實(shí)現(xiàn)Windows資源管理器的示例(附源碼),幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-03-03
  • C#為配置文件加密的實(shí)現(xiàn)方法

    C#為配置文件加密的實(shí)現(xiàn)方法

    這篇文章主要介紹了C#為配置文件加密的實(shí)現(xiàn)方法,可實(shí)現(xiàn)對(duì)配置文件中的敏感信息進(jìn)行加密,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源

    C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源

    本文主要介紹了C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02

最新評(píng)論