小喷菇和小喷菇的子弹创建
 
子弹
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShroomBullet : MonoBehaviour
{
    private Rigidbody2D rigidbody;
    private SpriteRenderer spriteRenderer;
    
    private int attackValue;
    
    private bool isHit;
    public void Init(int attackValue,Vector2 pos)
    {
        transform.position = pos;
        rigidbody = GetComponent<Rigidbody2D>();
        spriteRenderer = GetComponent<SpriteRenderer>();
        rigidbody.AddForce(Vector2.right*300);
        this.attackValue = attackValue;
        rigidbody.gravityScale = 0;
        isHit = false;
        
        spriteRenderer.sprite = GameManager.Instance.GameConf.ShroomBulletNor;
    }
    void Update()
    {
        if (isHit) return;
        if (transform.position.x>7.7F)
        {
            Destroy();
            return;
        }
    }
    private void OnTriggerEnter2D(Collider2D coll)
    {
        if (isHit) return;
        if (coll.tag == "Zombie")
        {
            isHit = true;
            
            AudioManager.Instance.PlayEFAudio(GameManager.Instance.GameConf.ZombieHurtForPea);
            
            coll.GetComponentInParent<ZombieBase>().Hurt(attackValue);
            
            spriteRenderer.sprite = GameManager.Instance.GameConf.ShroomBulletHit;
            
            rigidbody.velocity = Vector2.zero;
            
            Invoke("Destroy", 0.5f);
        }
    }
    private void Destroy()
    {
        
        CancelInvoke();
        
        PoolManager.Instance.PushObj(GameManager.Instance.GameConf.Bullet1,gameObject);
    }
}
 
小喷菇
 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class  PuffShroom : PlantBase
{
    public override float MaxHp
    {
        get
        {
            return 300;
        }
    }
    protected override float attackCD => 1.4f;
    protected override int attackValue => 20;
    
    private bool canAttack;
    
    private Vector3 creatBulletOffsetPos = new Vector2(0.562f, -0.386F);
    protected override void OnInitForPlace()
    {
        canAttack = true;
        
        InvokeRepeating("Attack", 0, 0.2f);
    }
    
    
    
    private void Attack()
    {
        if (canAttack == false) return;
        
        ZombieBase zombie = ZombieManager.Instance.GetZombieByLineMinDistance((int)currGrid.Point.y, transform.position);
        
        if (zombie == null) return;
        
        if (zombie.CurrGrid.Point.x == 8 && Vector2.Distance(zombie.transform.position, zombie.CurrGrid.Position) > 1.5f) return;
        
        if (zombie.transform.position.x < transform.position.x || zombie.transform.position.x - transform.position.x>5f ) return;
        
        
        ShroomBullet bullet = PoolManager.Instance.GetObj(GameManager.Instance.GameConf.ShroomBullet).GetComponent<ShroomBullet>();
        bullet.transform.SetParent(transform);
        bullet.Init(attackValue, transform.position + creatBulletOffsetPos);
        CDEnter();
        canAttack = false;
    }
    
    
    
    private void CDEnter()
    {
        StartCoroutine(CalCD());
    }
    
    
    
    IEnumerator CalCD()
    {
        yield return new WaitForSeconds(attackCD);
        canAttack = true;
    }
}