Unity实战案例全解析 :PVZ 植物脚本分析

植物都继承了Pants脚本,但是我因为没注意听讲,把Pants也挂在植物上了,所以子类的PlantEnableUpdate和PlantDisableUpdate抢不过父类,无法正确触发动画,我还找不到哪里出了问题,所以就使用了携程加while强行触发了,但是经过对源码和工程的分析比对,我发现了问题所在,所以写都写了就这样吧,又不是不能跑,而且协程优化还好一些吗,气死我了呜呜,耽误了好几个小时,下次我一定好好听课呜呜呜

植物状态脚本

cs 复制代码
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using Unity.VisualScripting;
using UnityEngine;
using static ControlState;

//植物状态枚举
enum PlantsState {
    Disable,
    Enable
}
public class Plants : MonoBehaviour {
  

    //拿到植物类型
    public PlantType plantType;
    PlantsState plantsState = PlantsState.Disable;
   

    private void Awake() {
    plantType = PlantType.Sun_Flower;
    Translate2Disable();
    }

    private void Update() {

        switch (plantsState) {
            case PlantsState.Disable:
                PlantDisableUpdate();
                break;
            case PlantsState.Enable:
                PlantEnableUpdate();
                break;
            default:
                break;
        }
    }

    protected virtual void PlantEnableUpdate() {

    }

    protected virtual void PlantDisableUpdate() {

    }

    public void Translate2Disable() {
        Debug.Log("关闭");
        plantsState = PlantsState.Disable;
        GetComponent<Animator>().enabled = false;
    }

    public void Translate2Enable() {
        Debug.Log("开启");
        plantsState = PlantsState.Enable;
        GetComponent<Animator>().enabled = true;
    }
}

向日葵脚本

cs 复制代码
using System.Collections;
using UnityEngine;
using System.Collections.Generic;
using Unity.VisualScripting;

public class SunFlower : Plants {
    private Animator animator;
    public GameObject sunPrefab; // 阳光预制体
    public float minTime = 4f; // 最小生成时间
    public float maxTime = 10f;//最大生成时间
    public float offsetY = -0.5f;
    public float offsetX;
    

    private void Awake() {
        animator = this.GetComponent<Animator>();
    }
    private void Start() {
        StartCoroutine(GenerateSun());
    }
    protected override void PlantEnableUpdate() {
        //if (!isGeneratingSun) {
        //    isGeneratingSun = true;
            
        //}
    }

    private IEnumerator GenerateSun() {
        while (true) {
            // 随机等待时间
            float waitTime = Random.Range(minTime, maxTime);
            yield return new WaitForSeconds(waitTime);

            animator.SetTrigger("CreatSun");
            yield return new WaitForSeconds(1f);
            // 随机生成阳光位置
            offsetX = Random.Range(-0.5f, 0.5f);
            Vector3 sunPosition = new Vector3(transform.position.x + offsetX, transform.position.y + offsetY, transform.position.z);
            GameObject sunInstance = Instantiate(sunPrefab, transform.position, Quaternion.identity);

            // 获取 Sun 组件并调用 JumpTo 方法
            Sun sunComponent = sunInstance.GetComponent<Sun>();
            if (sunComponent != null) {
                sunComponent.JumpTo(sunPosition);
            }
        }
    }
}

豌豆射手脚本

cs 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PeashShooter : Plants
{
    //攻击间隔
    public float atkTime = 2;
    //攻击计时
    public float atkTimer=0;
    //豌豆预制体
    public GameObject peach;

    //豌豆实例化位置
    public Transform peashPos;
    void Start() {
        PlantEnableUpdate();
        StartCoroutine(GenerateSun());
    }
 
    private IEnumerator GenerateSun() {
        while (true) {
            atkTimer += Time.deltaTime;
            if (atkTimer > atkTime) {
                atkTimer = 0;
                Shoot();
            }
            yield return new WaitForSeconds(0);
        }
}
    public void Shoot() {
        GameObject.Instantiate(peach, peashPos.transform.position,Quaternion.identity);
    }
}
相关推荐
一只一只10 小时前
Unity 3D笔记(进阶部分)——《B站阿发你好》
笔记·3d·unity·游戏引擎
yi碗汤园18 小时前
【一文了解】八大排序-冒泡排序、选择排序
开发语言·前端·算法·unity·c#·1024程序员节
qq_2052790521 小时前
unity PS 给图片换透明底图,去除白色底图
unity
AA陈超1 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-19 打开属性菜单
c++·游戏·ue5·游戏引擎·虚幻
向宇it1 天前
【推荐100个unity插件】unity易于使用模块化设计的天空、体积云和天气系统——Enviro 3
游戏·3d·unity·c#·游戏引擎
怣疯knight2 天前
unity实现2D人物从上面踩踏敌人,敌人减血的简易方法(类似马里奥的攻击手段)
unity·游戏引擎
AA陈超2 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-16 属性菜单 - 构建
c++·游戏·ue5·游戏引擎·虚幻
井队Tell3 天前
打造高清3D虚拟世界|零基础学习Unity HDRP高清渲染管线(第五天)
学习·3d·unity
yi碗汤园3 天前
【一文了解】八大排序-插入排序、希尔排序
开发语言·算法·unity·c#·1024程序员节
AA陈超3 天前
虚幻引擎5 GAS开发俯视角RPG游戏 P06-14 属性菜单 - 文本值行
c++·游戏·ue5·游戏引擎·虚幻