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;
  }

实现效果:

相关推荐
SmalBox1 小时前
【光照】[自发光Emission]以UnityURP为例
unity·渲染
SmalBox1 天前
【光照】Unity中的[经验模型]
unity·渲染
萘柰奈1 天前
Unity学习----【进阶】TextMeshPro学习(三)--进阶知识点(TMP基础设置,材质球相关,两个辅助工具类)
学习·unity
Yasin Chen1 天前
Unity UI坐标说明
ui·unity
应用市场1 天前
无人机姿态控制系统详解与实现
游戏引擎·cocos2d
陈言必行2 天前
Unity 性能优化 之 编辑器创建资源优化( 工作流 | 场景 | 预制体)
unity·编辑器·游戏引擎
1uther2 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
死也不注释2 天前
【Unity UGUI 交互组件——Slider(7)】
unity·游戏引擎·交互
程序猿多布2 天前
XLua教程之热补丁技术
unity·c#·lua·xlua
SmalBox2 天前
【光照】Unity中的[光照模型]概念辨析
unity·渲染