游戏场景——主玩家——属性奖励对象
奖励预制体
将其制作成预制体,按照这种方法制作血量、最大血量、攻击力、防御力等几个奖励。
写代码
加完后把自己移除
然后再为这些属性加上特效
ProReward的代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//奖励类型枚举
public enum E_ProType
{
atk,
def,
hp,
maxHP,
}
public class ProReward : MonoBehaviour
{
public E_ProType proType = E_ProType.def;
//默认的增加量
public int ProValue = 50;
//关联一个奖励特效
public GameObject getEff;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
PlayerObj player = other.GetComponent<PlayerObj>();
switch(proType)
{
case E_ProType.atk:
player.atk += ProValue;
break;
case E_ProType.def:
player.def += ProValue;
break;
case E_ProType.hp:
player.hp += ProValue;
//更新血条
GamePanel.Instance.UpdateHP(player.maxHP, player.hp);
break;
case E_ProType.maxHP:
player.maxHP = ProValue;
//更新血条
GamePanel.Instance.UpdateHP(player.maxHP, player.hp);
break;
}
//创建一个奖励特效
GameObject eff = Instantiate(getEff, transform.position, transform.rotation);
//控制特效的音效
AudioSource audioSource = eff.GetComponent<AudioSource>();
audioSource.volume = GameDataMgr.Instance.musicData.soundValue;
audioSource.mute = !GameDataMgr.Instance.musicData.isOpenSound;
audioSource.Play();
Destroy(gameObject);
}
}
}