Unity中实现转盘抽奖效果(二)

如果要使转盘停止时转到到指定位置,应该如何做?

实现思路:

也就是在需要停止的分数的区间范围内,随机一个角度值,然后反推需要在哪个角度开始减速,如果转盘的当前角度和需要开始减速的角度有差值,需要先匀速转到那个角度,然后开始匀减速到需要停止的位置。

实现代码:

计算需要匀速旋转角度的代码

cs 复制代码
   void RandReward()
   {
       float num = Random.Range(0f, 360f);
       Debug.Log(Mathf.FloorToInt(num / 45));
       //停止的时候 angularVelocity有速度,所以最终停的位置是
       //v = v0 - at -> t = v0/a
       //_angle = 0.5f * a * t * t = 0.5f * v0 * v0 /a;
       //得到要从哪个角度开始减速
       float angle1 = num - (0.5f * angularVelocity * angularVelocity / angularAcceleration % 360);
       if (angle1 < 0)
       {
           angle1 += 360;
       }
       startSpeedSlowVel = angle1 - angle;
       if (startSpeedSlowVel < 0)
       {
           startSpeedSlowVel += 360;
       }
   }

点击转盘结束时,需要更改的代码

cs 复制代码
  if(startSpeedSlowVel > 0)
  {
      startSpeedSlowVel -= angularVelocity * Time.deltaTime;
  }
  else
  {
      angularVelocity = Mathf.Max(0, angularVelocity - angularAcceleration * Time.deltaTime);
      startSpeedSlowVel = 0;
  }

实现效果:

相关推荐
郝学胜-神的一滴3 小时前
中级OpenGL教程 013:渲染器类架构设计与逐帧渲染流程详解
开发语言·c++·unity·游戏引擎·图形渲染·opengl·unreal
_helen_5208 小时前
unreal engine 虚拟现实 + airsim
游戏引擎·vr·虚幻
真鬼1232 天前
【Unity WebGL】内嵌网页与双向通信
unity·游戏引擎·webgl
zyh______2 天前
C#语法糖(按照实用性排序)
unity·c#
avi91112 天前
[AI教做人]CinemachineCamera 各项调整配置参数入门 + Profiler
unity·游戏开发·camera·cinemachine·visual camera·镜头配置
郝学胜-神的一滴3 天前
[简化版 GAMES 101] 计算机图形学 16:纹理走样、Mipmap、三线性插值、各向异性过滤原理全解
unity·游戏引擎·godot·图形渲染·three.js·opengl·unreal
2301_767113984 天前
Superpowers 游戏引擎从零开发实战指南
游戏引擎
吴梓穆4 天前
untiy TextMeshPro (TMP) text 操作 中文字符集 字体材质操作(添加纹理 添加描边 添加阴影)
unity
想你依然心痛4 天前
嵌入式单元测试:Unity/CMock框架与硬件在环测试——测试驱动、桩函数
unity·单元测试·游戏引擎
yangmu32034 天前
《星露谷物语》MOD配置与实战安装综合指南
游戏·游戏引擎·游戏程序