赞
踩
制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版13(完结)
制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版12
制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版11
制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版10
制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版9
制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版8
制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版7
制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版6
制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版5
制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版4
制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版3
制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版2
制作100个unity游戏之27】使用unity复刻经典游戏《植物大战僵尸》,制作属于自己的植物大战僵尸随机版和杂交版1
本节主要添加音效,添加新植物,火炬树桩和窝瓜
音效音乐管理器,之前文章有介绍过,具体可以看看:【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); } }
配置
动画配置事件,调用造成伤害事件
效果
源码不出意外的话我会放在最后一节
赠人玫瑰,手有余香!如果文章内容对你有所帮助,请不要吝啬你的点赞评论和关注
,以便我第一时间收到反馈,你的每一次支持
都是我不断创作的最大动力。当然如果你发现了文章中存在错误
或者有更好的解决方法
,也欢迎评论私信告诉我哦!
好了,我是向宇
,https://xiangyu.blog.csdn.net
一位在小公司默默奋斗的开发者,出于兴趣爱好,最近开始自学unity,闲暇之余,边学习边记录分享,站在巨人的肩膀上,通过学习前辈们的经验总是会给我很多帮助和启发!php是工作,unity是生活!如果你遇到任何问题,也欢迎你评论私信找我, 虽然有些问题我也不一定会,但是我会查阅各方资料,争取给出最好的建议,希望可以帮助更多想学编程的人,共勉~
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。