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

实现效果:

相关推荐
爱搞虚幻的阿恺12 天前
Niagara粒子系统-超炫酷的闪电特效(加餐 纸牌螺旋上升效果)
游戏·游戏引擎
_Li.12 天前
Simulink - 6DOF (Euler Angles)
人工智能·算法·机器学习·游戏引擎·cocos2d
weixin_4242946712 天前
Unity 调用Steamworks API 的 SteamUserStats.RequestCurrentStats()报错
unity·游戏引擎·steamwork
HoFunGames12 天前
Unity小地图,Easy Minimap System MT-GPS插件
unity·游戏引擎
wy32586436412 天前
Unity 新输入系统InputSystem(基本操作)
unity·c#·游戏引擎
WarPigs12 天前
着色器multi_compile笔记
unity·着色器
ECHO飞跃 01212 天前
Unity2019 本地推理 通义千问0.5-1.5B微调导入
人工智能·深度学习·unity·llama
Unity游戏资源学习屋12 天前
【Unity UI资源包】GUI Pro - Casual Game 专为休闲手游打造的专业级UI资源包
ui·unity
冰凌糕12 天前
Unity3D Shader 顶点法线外扩实现描边效果
unity
星和月12 天前
Untiy使用说明
c#·游戏引擎