Unity 経験を積んでレベルアップ

プレイヤーに経験値管理スクリプトを提供し
、敵の経験値を更新します
ここに画像の説明を挿入します

ここに画像の説明を挿入します

ここに画像の説明を挿入します

経験管理

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

public class LevelUPStats : MonoBehaviour
{
    
    // 等级 经验 显示等级 显示经验
    public int leve1 = 1;
    public float experience {
    
    get;private set;}
    public Text lv1Text;
    public Image expBarImage;
    
    public static int ExpNeedToLv1UP(int currentLeve1)
    {
    
    // 如果等于0的经验值等于0 并且增加现在的经验需要的经验
        if (currentLeve1 == 0)
            return 0;
        return (currentLeve1 * currentLeve1 + currentLeve1) * 5;
        
    }
    public void SetExperience(float exp)
    {
    
    // 获得经验增加经验 等级经验 
        experience += exp;
        float expNeeded = ExpNeedToLv1UP(leve1);
        float previousExperience = ExpNeedToLv1UP(leve1 - 1);
        
        if(experience >= expNeeded)
        {
    
    // 经验超过现在经验 升级 重置现在的经验和上一次获得的经验
            Leve1Up();
            expNeeded = ExpNeedToLv1UP(leve1);
            previousExperience = ExpNeedToLv1UP(leve1 - 1);

        }
        // 用以前的经验减去现在获得的经验
        expBarImage.fillAmount = (experience - previousExperience) / (expNeeded - previousExperience);
        // 当经验满了以后重新开始
        if (expBarImage.fillAmount == 1)
        {
    
    
            expBarImage.fillAmount = 0;
        }
    }
    public void Leve1Up()
    {
    
    // 等级升级 数字提升
        leve1++;
        lv1Text.text = leve1.ToString("");
    }
}

敵の情報

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

public class Stats : MonoBehaviour
{
    
    // 最大血量 当前血量 攻击数值 攻击间隔
    public float maxHealth;
    public float health;
    public float attackDmg;
    //public float attackSpeed;
    public float attackTime;

    HeroCombat heroCombatScript; // 英雄战斗脚本
    // 玩家变量 经验值
    private GameObject player;
    public float expVelue;
    
    void Start()
    {
    
       // 获得英雄战斗脚本
        heroCombatScript = GameObject.FindGameObjectWithTag("player").GetComponent<HeroCombat>();
        player = GameObject.FindGameObjectWithTag("player");
    }



    void Update()
    {
    
    // 当血量等于0
        if(health <= 0)
        {
    
    // 销毁对象 敌人为空 攻击停止
            Destroy(gameObject);
            heroCombatScript.targetedEnemy = null;
            heroCombatScript.perfoemMeleeAttack = false;
            // 杀死敌人获得规定的敌人的经验
            player.GetComponent<LevelUPStats>().SetExperience(expVelue);
        }
    }
}

おすすめ

転載: blog.csdn.net/qq_60839745/article/details/128751543