unity實(shí)現(xiàn)車方向盤轉(zhuǎn)動(dòng)效果
本文實(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#使用Object類實(shí)現(xiàn)棧的方法詳解
這篇文章主要介紹了C#使用Object類實(shí)現(xiàn)棧的方法,詳細(xì)分析了棧的原理及使用Object類實(shí)現(xiàn)棧的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2016-06-06
在Framework 4.0中:找出新增的方法與新增的類(二)
為什么動(dòng)態(tài)加載程序集無法找出Framework 4.0 和Framwork2.0 新增的方法和類2013-05-05
c# 將Datatable數(shù)據(jù)導(dǎo)出到Excel表格中
本文主要介紹了c# 將Datatable數(shù)據(jù)導(dǎo)出到Excel表格中的方法。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-03-03
c# WPF實(shí)現(xiàn)Windows資源管理器(附源碼)
這篇文章主要介紹了c# WPF實(shí)現(xiàn)Windows資源管理器的示例(附源碼),幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03
C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源
本文主要介紹了C#快速實(shí)現(xiàn)IList非泛型類接口的自定義類作為數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02

