当前位置: 首页 > article >正文

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

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

植物状态脚本 

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;
    }
}

向日葵脚本

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);
            }
        }
    }
}

豌豆射手脚本

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);
    }
}


http://www.kler.cn/news/308680.html

相关文章:

  • linux git配置kdiff3工具解决冲突
  • C语言11--特殊函数
  • git删除本地分支报错:error: the branch ‘xxx‘ is not fully merged
  • 1分钟解决 -bash: mvn: command not found,在Centos 7中安装Maven
  • 【代码随想录训练营第42期 Day58打卡 - 图论Part8 - 拓扑排序
  • 微信小程序----日期时间选择器(自定义时间精确到分秒)
  • Redis -- 全记录(面试)
  • tensor连接和拆分
  • mysql学习教程,从入门到精通,MySQL 子查询 子句(11)
  • 高级I/O知识分享【epoll || Reactor ET,LT模式】
  • Numba基础
  • vue.nextTick()方法的使用
  • python怎么运行cmd命令
  • 网络协议头分析
  • 【PostgreSQL-patroni维护命令】
  • 基于vue框架的宠物寄养系统3d388(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
  • USB开启ADB设置流程
  • 麒麟操作系统 MySQL 主从搭建
  • Qt QDialog点击界面自动激活问题解决办法
  • 枚举类题目练习心得
  • Golang | Leetcode Golang题解之第403题青蛙过河
  • 【题解】CF2009G1
  • QtC++截图支持获取鼠标光标
  • 运维工程师面试整理-虚拟化与容器
  • 实时数仓3.0DWD层
  • vulnhub(7):Toppo(经典的suid滥用提权)
  • ArcGIS Pro SDK (十四)地图探索 1 地图视图
  • 探索 InternLM 模型能力边界
  • 什么是外贸专用路由器?
  • 后端开发 每天六道面试题之打卡第一天