当前位置:   article > 正文

【Unity植物大战僵尸】豌豆射手子弹发射逻辑(十三)

unity植物大战僵尸

23、子弹导入及其初始化

导入子弹素材
在Bullet游戏对象上添加相关组件
既然子弹具有碰撞体,那么僵尸也得有碰撞体
新建Zombie的Tag(以便确认碰撞对象是否是僵尸群体,这里用的Tag,其实图层也可以),然后将触发器勾上,以便实现碰撞的一些逻辑,而不是真实的碰撞
添加子弹击中效果
在Bullet.cs中添加代码
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class Bullet : MonoBehaviour
  6. {
  7.     private Rigidbody2D rigidbody;
  8.     private SpriteRenderer spriteRenderer;
  9.     // 是否击中
  10.     private bool isHit = false;
  11.     void Start()
  12.     {
  13.         rigidbody = GetComponent<Rigidbody2D>();
  14.         spriteRenderer = GetComponent<SpriteRenderer>();
  15.         //  施加一个向右的力,匀速运动
  16.         rigidbody.AddForce(Vector2.right*300);
  17.     }
  18.     
  19.     void Update()
  20.     {
  21.         if (isHit) return;
  22.         // 朝Z轴旋转
  23.         transform.Rotate(new Vector3(0,0,-15f));
  24.     }
  25.     private void OnTriggerEnter2D(Collider2D other)
  26.     {
  27.         if (other.tag == "Zombie")
  28.         {
  29.             isHit = true;
  30.             // 击中后修改成击中图片
  31.             spriteRenderer.sprite = GameManager.instance.GameConf.BulletHit;
  32.             // 暂停自己的运动
  33.             rigidbody.velocity = Vector2.zero;
  34.             // 恢复重力下落
  35.             rigidbody.gravityScale = 1;
  36.             // 最后销毁(延迟调用)
  37.             Invoke("Destroy",0.5f);
  38.         }
  39.     }
  40.     private void Destroy()
  41.     {
  42.         Destroy(gameObject);
  43.     }
  44. }
测试

24、子弹逻辑完善

首先将子弹保存成预制体,并放在GameConf中
新建ZombieManager.cs脚本来管理僵尸
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. public class ZombieManager : MonoBehaviour
  6. {
  7.     public static ZombieManager Instance;
  8.     private void Awake()
  9.     {
  10.         Instance = this;
  11.     }
  12.     private List<Zombie> zombies = new List<Zombie>();
  13.     public void AddZombie(Zombie zombie)
  14.     {
  15.         zombies.Add(zombie);
  16.     }
  17.     
  18.     /// <summary>
  19.     /// 获取一个距离最近的僵尸
  20.     /// </summary>
  21.     /// <param name="lineNum">植物当前的行</param>
  22.     /// <param name="pos">植物当前的坐标</param>
  23.     /// <returns></returns>
  24.     public Zombie GetZombieBylineMinDistance(int lineNum, Vector3 pos)
  25.     {
  26.         Zombie zombie = null;
  27.         float dis = 10000;
  28.         for (int i = 0; i < zombies.Count; ++i)
  29.         {
  30.             // 遍历所有僵尸中等于传进来的lineNum(植物当前的行),并且返回最小距离的僵尸
  31.             if ((int)zombies[i].CurrGrid.Point.y == lineNum && Vector2.Distance(pos, zombies[i].transform.position) < dis)
  32.             {
  33.                 dis = Vector2.Distance(pos, zombies[i].transform.position);
  34.                 zombie = zombies[i];
  35.             }
  36.         }
  37.         return zombie;
  38.     }
  39. }
并在Zombie.cs中添加代码
最后在PeaShooter.cs中添加代码
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. /// <summary>
  5. /// 植物的基类
  6. /// </summary>
  7. public class PeaShooter : PlantBase
  8. {
  9.     public override float MaxHp
  10.     {
  11.         get { return 300; }
  12.     }
  13.     // 是否可以攻击
  14.     private bool canAttack = true;
  15.     // 攻击间隔
  16.     private float attackCD = 1.4f;
  17.     // 实例化子弹相对于豌豆射手的偏移量
  18.     private Vector3 createBulletOffestPos = new Vector2(0.562f0.396f);
  19.     
  20.     protected override void OnInitForPlace()
  21.     {
  22.         // 每0.2s攻击
  23.         InvokeRepeating("Attack",0,0.2f);
  24.     }
  25.     
  26.     /// <summary>
  27.     /// 攻击方法-循环检测
  28.     /// </summary>
  29.     private void Attack()
  30.     {
  31.         if (canAttack == falsereturn;
  32.         // 从僵尸管理器获取一个离我最近的僵尸
  33.         Zombie zombie = ZombieManager.Instance.GetZombieBylineMinDistance((int) currGrid.Point.y, transform.position);
  34.         // 前方没有僵尸则跳出
  35.         if (zombie == nullreturn;
  36.         // 僵尸必须在草坪上
  37.         if ((int)zombie.CurrGrid.Point.x == 8 &&
  38.             Vector2.Distance(zombie.transform.position, zombie.CurrGrid.Position) > 1.5freturn;
  39.         // 此使开始发射,实例化一个子弹
  40.         Bullet bullet = GameObject.Instantiate<GameObject>(GameManager.instance.GameConf.Bullet1,
  41.             transform.position + createBulletOffestPos, Quaternion.identity, transform).GetComponent<Bullet>();
  42.         CDEnter();
  43.         canAttack = false;
  44.     }
  45.     
  46.     private void CDEnter()
  47.     {
  48.         // 开始计算冷却
  49.         StartCoroutine(CalCD());
  50.     }
  51.     // 计算冷却时间
  52.     IEnumerator CalCD()
  53.     {
  54.         yield return new WaitForSeconds(attackCD);
  55.         // 冷却时间结束
  56.         canAttack = true;
  57.     }
  58.     
  59. }

测试

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/秋刀鱼在做梦/article/detail/770455
推荐阅读
相关标签
  

闽ICP备14008679号