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

实现效果:

相关推荐
郝学胜-神的一滴5 小时前
基于OpenGL封装摄像机类:视图矩阵与透视矩阵的实现
c++·qt·线性代数·矩阵·游戏引擎·图形渲染
EQ-雪梨蛋花汤9 小时前
【Unity笔记】Unity 编辑器扩展:打造一个可切换 Config.assets 的顶部菜单插件
unity·编辑器·游戏引擎
SmalBox10 小时前
【URP】UnityHLSL顶点片元语义详解
unity·渲染
在路上看风景21 小时前
9. Mono项目与Unity的关系
unity
在路上看风景1 天前
1.12 Memory Profiler Package - Summary
unity
SmalBox1 天前
【URP】Unity Shader Tags
unity·渲染
极客柒1 天前
Unity 塔防自用可视化路点寻路编辑器
unity·编辑器·游戏引擎
程序猿多布1 天前
Unity AssetBundle详解
unity·assetbundle
萘柰奈2 天前
Unity学习----【进阶】Addressables(二)--加载资源与打包及更新
学习·unity
lvcoc2 天前
unity 接入火山引擎API,包括即梦AI
windows·unity·ai·火山引擎