Unity实现大转盘的简单笔记

这篇文章主要为大家分享了Unity实现大转盘的简单笔记,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Unity实现大转盘展示的具体代码,供大家参考,具体内容如下

1、unity中要实现旋转一个gameobject,我们需要改变它的transform下对应的Rotation,由于我们的大转盘是2D的视角,所以我们首先需要明确大转盘旋转的方向是旋转Rotation的Z。

2、如何实现大转盘由旋转快到慢,再到旋转指定为位置停下。查看了unity的脚本可以找到如下方法实现旋转大转盘如下:

 public void Rotate(Vector3 eulerAngles, Space relativeTo = Space.Self);

应用一个欧拉角的旋转角度,eulerAngles.z度围绕z轴,eulerAngles.x度围绕x轴,eulerAngles.y度围绕y轴(这样的顺序)。

 public void Rotate(Vector3 axis, float angle, Space relativeTo = Space.Self);

按照angle度围绕axis轴旋转变换。

 public void RotateAround(Vector3 point, Vector3 axis, float angle);

简单的说,按照多少度在世界坐标的某位置轴旋转物体

3、对于旋转查看unity脚本知道以上方法都是需要在void Update();方法先实现的。所以我们在void Update()方法实现:

 //是否点击开始按钮 if (m_isStart == true) { m_cStartButton.m_CurState = CGUIAdvancedButton.AdvancedButtonState.Disable;//当前开始按钮状态为禁用 m_fTime += Time.deltaTime;//时间计时器累加 m_fVelocity = k / m_fTime;//播放速度(我们使用反比例函数可以实现速度的由快到慢的效果) if (m_fVelocity<=1) { float value = (m_iID*36.0f);//此处为我的项目指定的停止的地方 float diff = m_gRatePin.transform.eulerAngles.z - value;//当前旋转gameobject的欧拉角 if (Mathf.Abs(diff)<=50)//如果旋转的gameobject和指定的停止位置小于50度则开始缓慢的减速 { //使用球形插值可以让旋转的gameobject缓慢的转到制定位置 Quaternion quaternion = m_gRatePin.transform.rotation; m_gRatePin.transform.rotation = Quaternion.Slerp(quaternion, Quaternion.Euler(0, 0, value), Time.deltaTime); if (m_gRatePin.transform.rotation == Quaternion.Euler(0, 0, value)) { m_isStart = false; m_cStartButton.m_CurState = CGUIAdvancedButton.AdvancedButtonState.Normal; m_fTime = 0; m_fVelocity = 36.0f; } } else { //旋转(-1表示方向为右) m_gRatePin.transform.Rotate(Vector3.forward, (-1) * m_fVelocity); } } else { //旋转(-1表示方向为右) m_gRatePin.transform.Rotate(Vector3.forward, (-1) * m_fVelocity); } } 

4、除了使用旋转gameobject达到实现大转盘旋转的我们还可以使用动画实现如下:

 if (m_isStart == true)       //是否点击开始抽奖按钮 { m_cStartButton.m_CurState = CGUIAdvancedButton.AdvancedButtonState.Disable; m_fTime += Time.deltaTime;     //计时器 if (m_fTime > 1 / m_fVelocity)    //判断当前时间是否到达了播放一帧的时间 { m_fTime -= 1 / m_fVelocity; if (m_fVelocity<=8f)     //判断速度是否小于某个值(根据自己的需要设定) { if (index == m_iID)    //判断当前播放的帧是否为后台控制数据,如果是则停止播放并且清空数据 { m_gRatePin.GetComponent().sprite = m_sSprites[m_iID]; m_isStart = false; m_fVelocity = 60.0f; m_fTime = 0.0f; m_fTimeMeter = 0.0f; m_iCount = 0; m_cStartButton.m_CurState = CGUIAdvancedButton.AdvancedButtonState.Normal; } else { //1 / m_fVelocity表示播放一帧需要多少时间 if (index == m_sSprites.Length - 1)    //如果当前帧数索引为最后一帧,说明播放的下一帧为第一帧 { m_gRatePin.GetComponent().sprite = m_sSprites[0]; index = 0;         //重置索引 m_iCount++;         //统计圈数 m_fVelocity /= 2;       //减缓播放帧速度 } else { //如果不是的话就继续播放下一帧 m_gRatePin.GetComponent().sprite = m_sSprites[++index]; } } } else//没有达到指定圈则继续播放下一帧 { if (index == m_sSprites.Length - 1) { m_gRatePin.GetComponent().sprite = m_sSprites[0]; index = 0; m_iCount++; m_fVelocity /= 2; } else { m_gRatePin.GetComponent().sprite = m_sSprites[++index]; } } } }

5、其实还有蛮多的方法例如使用unity自己带的动画系统也是可以实现的,笔记总结到此。

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

以上就是Unity实现大转盘的简单笔记的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程