zl程序教程

您现在的位置是:首页 >  其它

当前栏目

U3D实现连击效果

实现 效果
2023-09-11 14:19:57 时间

请添加图片描述

@作者 : SYFStrive

@博客首页 : HomePage

📌:个人社区(欢迎大佬们加入) 👉:社区链接🔗

📌:觉得文章不错可以点点关注 👉:专栏连接🔗

请添加图片描述

相关专栏

👉 U3D案例库(🔥)

连击好处

  1. 连击是指在游戏中进行持续的操作。 在FTG类、ARPG类等游戏中,有的游戏设有连击系统。由于连击衔接的时间间隔在系统上有一定的设定,所以一般而言,连击是需要一定的技巧的。所以,在某些游戏中,连击的衔接及次数是衡量触手与否的重要标准。
  2. 攻击是一个靠普攻发挥威力的特技,进行普攻才会触发连击,所以如果使用的战法漏气了,还有连击效果来弥补,效果依然还是很可观的。

实现功能

案例说明:通过UI实现连击效果 👉 通过携程模拟连击效果的时间间隔 👉 通过DOTween、LeanTween简单完成连击的动画

插件下载

在这里插入图片描述

在这里插入图片描述

引入命名空间

DOTween需要引入命名空间 、LeanTween不需要引入命名空间直接使用、如 👇

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

使用到的变量

    //分值
    public int score { get; private set; }
    [Header("===连击的值、时间间隔===")]
    public float doubleScore;
    private float intervalTime = 2;
    private float lastTime;

    [Space]
    [Header("===连击的Spri、连击的Text===")]
    public Image imageSpr;
    public GameObject doubleScoreText;

    [Space]
    [Header("===连击分值效果===")]
    [SerializeField] float fromScore;
    [SerializeField] float toScore;
    [SerializeField] float animationTime;
    [SerializeField] Text scoreText;

初始化代码

public class ComboEffect : MonoBehaviour
{
    private void Start()
    {
        score = 0;
        lastTime = 0;
        doubleScoreText.gameObject.SetActive(false);
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            DoubleHitDoubleScore();
        }
    }
}

完成连击值效果

    #region 连击值效果

    private void DoubleHitDoubleScore()
    {
        if (Time.time <= lastTime + intervalTime)
        {
            doubleScore++;
        }
        else
        {
            doubleScore = 0;
            doubleScore++;
            imageSpr.fillAmount = 1;
        }

        doubleScoreText.gameObject.SetActive(true);
        doubleScoreText.transform.DOScale(Vector2.one * 3f, 0.2f).OnComplete(() => {
            doubleScoreText.transform.DOScale(Vector2.one, 0.2f);
        });
        StartCoroutine(nameof(ImageFillAmountDoubleHit));
        doubleScoreText.GetComponent<Text>().text =doubleScore * 1 + "连击";
        lastTime = Time.time;
        DoublehitScore();
    }

    IEnumerator ImageFillAmountDoubleHit()
    {
        float doubleValue = 1;
        float currentScore = doubleScore;

        while (doubleValue > 0)
        {
            if (currentScore == doubleScore)
            {
                doubleValue -= Time.deltaTime / intervalTime;
                imageSpr.fillAmount = doubleValue;
            }
            else
            {
                StopCoroutine(nameof(ImageFillAmountDoubleHit));
                StartCoroutine(nameof(ImageFillAmountDoubleHit));
            }
            yield return null;
        }
        doubleScore = 0;
        doubleScoreText.gameObject.SetActive(false);
    }
    #endregion

完成连击分值效果

    #region 连击分值效果

    public void DoublehitScore()
    {
        fromScore = score;

        toScore = fromScore + doubleScore * 100;

        //参数:开始值、目标值、渐变的时间
        LeanTween.value(fromScore, toScore,animationTime)
            .setEase(LeanTweenType.easeInOutQuint)
            .setOnUpdate((float _value) => { fromScore = _value; scoreText.text ="分值:"+fromScore.ToString("f6");});
        score = (int)toScore;
    }
    #endregion

效果

在这里插入图片描述

在这里插入图片描述
本文到这里就结束了,大佬们的支持是我持续更新的最大动力,希望这篇文章能帮到大家💪

 

                 相关专栏连接🔗
在这里插入图片描述

下篇文章再见ヾ( ̄▽ ̄)ByeBye

在这里插入图片描述