查看: 696|回复: 0
打印 上一主题 下一主题

[其他] 收藏的帖子 — 跳跃设计

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53488
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2016-1-18 05:30:02 |只看该作者 |倒序浏览
Cloud Critters是一个跳跃类的游戏。我们的目的是将一些类似《几何大战》这种具有潜质的游戏同《Flappy Bird》和《Don’t Touch the Spikes》这些单点跳跃游戏进行细致的融合。

在《Flappy Bird》中,用户每次点击都会使小鸟上升一个固定的高度。我们在游戏《Cloud Critters》开发前期就决定可以控制角色跳跃的高度,就和《超级玛丽》或者《密特罗德》一样。这样将会给玩家更多的操作空间。

在进入如何实现这样一个可以正确控制跳跃高度的之前,我们先看一下这一段代码,之后的示例都将基于它。

注意:这里为了示范方便,我们将在Update函数中启用跳跃协程(JumpRoutine)。

  1. void Update()
  2. {
  3.         if(jumpButtonDown&& !jumping)
  4.         {
  5.                 jumping= true;
  6.                 StartCoroutine(JumpRoutine());
  7.         }
  8. }
复制代码
在实际的工程中,你最好在FixedUpdate中处理物理运动相关的行为。
匀加速情况:
  1. {
  2. rigidbody.velocity= Vector2.zero;
  3. floattimer = 0;
  4. while(jumpButtonPressed &&timer < jumpTime)
  5. {
  6. //Adda constant force every frame of the jump
  7. rigidbody.AddForce(jumpVector);
  8. timer+= Time.deltaTime;
  9. yieldreturn null;
  10. }
  11. jumping= false;
  12. }
复制代码
优点:
跳跃高度的最小最大值差距较大,玩家可以更好的控制角色跳跃的高度和距离。
缺点:
每一帧都给角色施以一个固定的外力,导致角色在竖直方向上的速度会越来越快,就像背着一个喷气装置。

类抛物情况:
  1. IEnumeratorJumpRoutine()
  2. {
  3. //Setthe gravity to zero and apply the force once
  4. floatstartGravity = rigidbody.gravityScale;
  5. rigidbody.gravityScale= 0;
  6. rigidbody.velocity= jumpVector;
  7. floattimer = 0f;
  8. while(jumpButtonPressed&& timer < jumpTime)
  9. {
  10. timer+= Time.deltaTime;
  11. yieldreturn null;
  12. }
  13. //Setgravity back to normal at the end of the jump
  14. rigidbody.gravityScale= startGravity;
  15. jumping= false;
  16. }
复制代码
优点:
在跳起的过程中不会一直加速。在开始跳跃的那一刻,角色就会获得最大的跳跃速度,当用户松开按键或者达到跳跃最大高度后,便落下。这种跳跃形式和《超级玛丽》 比较接近。

缺点:
在玩家松开跳跃键后,角色还会持续几帧跳跃动画,继续上升直到向上的速度降为零才下落。这意味着即使你只是快速按一下跳跃键,角色也会跳起相当高的一端距离。


类抛物情况改进版:
  1. IEnumeratorJumpRoutine()
  2. {
  3. //Addforce on the first frame of the jump
  4. rigidbody.velocity= Vector2.zero;
  5. rigidbody.AddForce(jumpVector,ForceMode2D.Impulse);
  6. //Waitwhile the character's y-velocity is positive (the character is going
  7. //up)
  8. while(jumpButtonPressed&& rigidbody.velocity.y > 0)
  9. {
  10. yieldreturn null;
  11. }
  12. //Ifthe jumpButton is released but the character's y-velocity is still
  13. //positive...
  14. if(rigidbody.velocity.y> 0)
  15. {
  16. //...setthe character's y-velocity to 0;
  17. rigidbody.velocity= new Vector2(rigidbody.velocity.x, 0);
  18. }
  19. jumping= false;
  20. }
复制代码
优点:
能很精确地控制跳跃的最高点,当跳跃按钮松开时,角色即刻下落。

缺点:
尽管说此种跳跃达到最远跳跃距离时,角色的运动轨迹是一条平滑的弧线,可是当我们没有跳到最远距离时,这条运动轨迹就会出现较为明显的波动。

终极版本:
  1. IEnumerator JumpRoutine()
  2. {
  3.         rigidbody.velocity= Vector2.zero;
  4.         float timer = 0;
  5.         while(jumpButtonPressed&& timer < jumpTime)
  6.         {
  7.                 //Calculatehow far through the jump we are as a percentage
  8.                 //applythe full jump force on the first frame, then apply less force
  9.                 //eachconsecutive frame
  10.                 float proportionCompleted = timer / jumpTime;
  11.                 Vector2 thisFrameJumpVector = Vector2.Lerp(jumpVector, Vector2.zero,proportionCompleted);
  12.                 rigidbody.AddForce(thisFrameJumpVector);
  13.                 timer+= Time.deltaTime;
  14.                 yieldreturn null;
  15.         }
  16.         jumping= false;
  17. }
复制代码
优点:
玩家可以很好地控制角色跳跃的高度,并且跳跃过程中的运动轨迹都是很平滑的弧线,也不会出现类似喷气式上升的情况。
缺点:
没有缺点,简直完美!

分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-8-16 16:29 , Processed in 0.058545 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部