在 Unity 中使用 DOTween 实现 A Dark Room 中的战斗效果

思路:

  1. 使用DOTween的DOPunchAnchorPos方法移动UI;
  2. 使用DOTween的DOFade方法实现被攻击掉落生命值时的淡入淡出效果。
Unity实现的战斗效果.gif

实现代码:

BattleController.cs
/// <summary> 攻击 </summary>
    void Attack(RectTransform attacker, Vector2 directionAndstrength, Text suffererLossHPText, Text sufferCurrentHPText, int lossHP, ref int suffererCurrentHP)
    {
        if (!m_isBattleOver)
        {
            // 攻击动画
            attacker.DOPunchAnchorPos(directionAndstrength, 0.2f, 0, 0f, false);
            // 受攻击一方生命值减小,减小值为攻击方的攻击力
            suffererCurrentHP -= lossHP;

            m_battleView.SetCurrentHP(sufferCurrentHPText, suffererCurrentHP);
            // Debug.Log("currentHP:" + suffererCurrentHP);
            // 受攻击一方显示生命值减小的值
            suffererLossHPText.text = "-" + lossHP.ToString();
            // 调用减小生命值动画效果
            m_battleView.LossHPAnimation(suffererLossHPText);
        }

    }

RectTransform (Unity UI 4.6)

DOPunchAnchorPos(Vector2 punch, float duration, int vibrato, float elasticity, boolsnapping)

Punches the target's anchoredPosition with the given values.
punch: punch The direction and strength of the punch (added to the RectTransform's current position).
duration: The duration of the tween.
vibrato: Indicates how much will the punch vibrate.
elasticity: Represents how much (0 to 1) the vector will go beyond the starting position when bouncing backwards. 1 creates a full oscillation between the punch direction and the opposite direction, while 0 oscillates only between the punch and the start position.
**snapping: ** If TRUE the tween will smoothly snap all values to integers.

BattleView.cs
/// <summary> 被攻击时掉落生命值动画效果 </summary>
    public void LossHPAnimation(Text lossHPText)
    {
        // FadeIn
        lossHPText.DOFade(1f, 0f);
        // FadeOut
        lossHPText.DOFade(0f, 1.5f);
    }

Text (Unity UI 4.6)

DOFade(float to, float duration)

Fades the target's alpha to the given value.

Unity5.4, C#
Github

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,917评论 0 23
  • 第13讲 分为三类 1 爱自己 分析自己现金流和真正的需求 2 靠自己 设立一个清晰可行的目标。用挑包包的眼光,挑...
    何小吱阅读 217评论 0 2
  • 每天上学,我是如此的烦恼,一看到毕老师那张那张凶巴巴的脸我就我的心情就糟透了,他成天就爱板着脸就训人。多了个没...
    A王佳耀阅读 595评论 0 0
  • JS 中的继承主要分为两种:原型继承和非原型继承,非原型继承又分为寄生方式继承、借用或伪造构造函数方式继承、组合方...
    DaZzling_佳阅读 266评论 0 0
  • Flask扩展详细说明文档 http://flask.pocoo.org/extensions/ Flask表单w...
    cexpert阅读 182评论 0 1