Unity2D 角色生命系统及UI控制脚本编写之三(学习自用)

已经设置完角色的ui界面后,要对角色设置进入无敌模式。

在PlayerHealthController脚本种设置变量

public  float invincibleLenght;    // 声明玩家无敌时间变量
private float invincibleCounter;   // 声明玩家无敌计数器

在脚本种设置变量为1。

当角色触碰到尖刺时,无敌时间开始从1慢慢变到0

void Update(){
    if(invincibleCounter > 0){
        invincibleCounter -= Time.deltaTime;
    }
}

编写函数,把无敌长度赋予给计时器,当计时器小于0时,如果角色的生命值减一,那么角色的无敌计时器等于1。

    public void DealDamage(){
        
            currentHealth --;
            
            // 当生命值为0时,玩家消失
            if(currentHealth <= 0){
                gameObject.SetActive(false);
            }
            else{
                invincibleCounter = invincibleLenght;
            }
    }

再设置一个限定条件,如果无敌计时器不等于0的时候,那么角色的生命值不会减一。这里设置if语句。

    public void DealDamage(){
        
        if(invincibleCounter <= 0)
        {
        
            currentHealth --;
            
            // 当生命值为0时,玩家消失
            if(currentHealth <= 0){
                gameObject.SetActive(false);
            }
            else{
                invincibleCounter = invincibleLenght;
            }
        }
    }

之后给角色颜色设置透明度。

theSR.color = new Color(theSR.color.r, theSR.color.g, theSR.color.b, 0.5f);

首先设置变量

private SpriteRenderer theSR;   // 声明玩家渲染变量

这是一个私有变量,所以在脚本先对变量获取

    void Start()
    {
        currentHealth = maxHealth;

        theSR = GetComponent<SpriteRenderer>();
    }

在update中,如果角色的无敌计时器小于0,那么角色一定是不透明的。

    void Update()
    {
        if(invincibleCounter > 0){
            invincibleCounter -= Time.deltaTime;

            if(invincibleCounter <= 0){
                theSR.color = new Color(theSR.color.r, theSR.color.g, theSR.color.b, 1f);
            }
        }
    }

之后调用ui界面

UIController.instance.UpdateHealthDisplay();

完整代码如下

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerHealthController : MonoBehaviour
{
    public static PlayerHealthController instance;   // 声明静态变量

    public int currentHealth, maxHealth;             // 声明当前生命和最大生命

    public  float invincibleLenght;                  // 声明玩家无敌时间变量
    private float invincibleCounter;                 // 声明玩家无敌计数器

    private SpriteRenderer theSR;                    // 声明玩家渲染变量

    private void Awake()
    {
        instance = this;
    }

    void Start()
    {
        currentHealth = maxHealth;

        theSR = GetComponent<SpriteRenderer>();
    }


    void Update()
    {
        if(invincibleCounter > 0){
            invincibleCounter -= Time.deltaTime;

            if(invincibleCounter <= 0){
                theSR.color = new Color(theSR.color.r, theSR.color.g, theSR.color.b, 1f);
            }
        }
    }

    public void DealDamage(){
        
        if(invincibleCounter <= 0)
        {
        
            currentHealth --;
            
            // 当生命值为0时,玩家消失
            if(currentHealth <= 0){
                gameObject.SetActive(false);
            }
            else{
                invincibleCounter = invincibleLenght;
                theSR.color = new Color(theSR.color.r, theSR.color.g, theSR.color.b, 0.5f);
            }

            // 调用静态变量函数
            UIController.instance.UpdateHealthDisplay();
        }
    }
}

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值