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 分钟前
Unity3D大规模点击检测:GPU Picking vs MeshCollider + Raycast
unity
在路上看风景14 分钟前
1.4 Unity运行时路径
unity·游戏引擎
郝学胜-神的一滴1 小时前
Qt OpenGL 生成Mipmap技术详解
开发语言·c++·qt·系统架构·游戏引擎·图形渲染·unreal engine
孟无岐20 小时前
【Laya】Laya 类使用说明
typescript·游戏引擎·游戏程序·laya
在路上看风景1 天前
1.2 Unity资源分类
unity·游戏引擎
one named slash1 天前
BMFont在Unity中生成艺术字
unity·游戏引擎
郝学胜-神的一滴1 天前
图形学中的纹理映射问题:摩尔纹与毛刺的深度解析
c++·程序人生·unity·游戏引擎·图形渲染·unreal engine
在路上看风景1 天前
10. CPU-GPU协作渲染
unity
程序员agions1 天前
Unity 游戏开发邪修秘籍:从入门到被策划追杀的艺术
unity·cocoa·lucene
JIes__1 天前
Unity(一)——场景切换、退出游戏、鼠标隐藏锁定...
unity·游戏引擎