前言
本节主要添加音效,添加新植物,火炬树桩和窝瓜
添加音效
音效音乐管理器,之前文章有介绍过,具体可以看看:【unity小技巧】Unity音乐和音效管理器
火炬树桩
卡牌和预制体和前面配置类似,配置火焰子弹动画
新增火炬树桩代码Torchwood
public class Torchwood : Plants { }
挂载脚本
修改PeaBullet
public int damage;//伤害 //进入触发器 private void OnTriggerEnter2D(Collider2D other) { //触碰火炬树桩变为火豆,伤害翻倍 if (other.gameObject.GetComponent<Torchwood>()) { animator.SetBool("isFire", true); damage = damage * 2; } if (other.CompareTag("Enemy")) { other.GetComponent<Character>()?.TakeDamage(damage); AudioManager.Instance.PlaySFX("豌豆击中"); } }
去除原来子弹的Attack代码,配置参数,并添加刚体,不然碰撞不会生效
记得检查2d物理碰撞是否配置正确
效果
可以加点命中火焰效果优化细节,修改PeaBullet
//进入触发器 private void OnTriggerEnter2D(Collider2D other) { //触碰火炬树桩变为火豆,伤害翻倍 if (other.gameObject.GetComponent<Torchwood>()) { animator.SetBool("isFire", true); damage = damage * 2; isFire = true; } if (other.CompareTag("Enemy")) { other.GetComponent<Character>()?.TakeDamage(damage); AudioManager.Instance.PlaySFX("豌豆击中"); //生成火焰效果 if(isFire){ GameObject firePrefab = Resources.Load("Prefabs/特效/火焰特效") as GameObject; GameObject go = Instantiate(firePrefab); go.transform.parent = other.gameObject.transform; go.transform.localPosition = Vector2.zero + new Vector2(0, -25); } } }
效果
窝瓜
卡牌和预制体和前面配置类似
配置动画
新增窝瓜代码Squash
public class Squash : Plants { public int damage;//伤害 private GameObject targetEnemy;//目标敌人 private Animator animator; bool attack; private void Start() { animator = GetComponent<Animator>(); } private void Update() { if(attack) transform.position = Vector2.MoveTowards(transform.position, targetEnemy.transform.position + new Vector3(0, -50, 0), 200 * Time.deltaTime); } //进入触发器 private void OnTriggerEnter2D(Collider2D other) { if (other.CompareTag("Enemy")) { GetComponent<Collider2D>().enabled = false; targetEnemy = other.gameObject; AudioManager.Instance.PlaySFX("窝瓜触发"); Invoke("SetAttack", 1f); } } private void SetAttack(){ attack = true; animator.SetTrigger("attack"); } //造成伤害 public void SetDamage(){ targetEnemy.GetComponent<Character>()?.TakeDamage(damage); AudioManager.Instance.PlaySFX("窝瓜攻击"); Destroy(gameObject, 0.5f); } }
配置
动画配置事件,调用造成伤害事件
效果
源码
源码不出意外的话我会放在最后一节