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

Unity3D仿星露谷物语开发47之砍树时落叶特效

1、目标

当橡树被砍伐时的落叶粒子效果。

2、创建粒子物体

Hierarchy -> PersistentScene下创建新物体命名为DeciduousLeavesFalling。

添加Particle System组件。

基础配置如下:(暂时勾选Looping实时可以看生成效果,后面反选即可)

配置Emission和Shape信息如下:

配置Color over Lifetime:

配置Size over Lifetime / Rotation over Lifetime / Noise 信息如下:

配置Texture / Renderer 信息如下:

将DeciduousLeavesFalling移到Assets -> Prefabs -> Crop -> Effect下

3、修改Crop.cs脚本

[Tooltip("This should be populated from child transform gameobject showing harvest effect spawn point")]
[SerializeField] private Transform harvestActionEffectTransform = null;

在ProcessToolAction函数中添加:

完整代码为:

public void ProcessToolAction(ItemDetails equippedItemDetails, bool isToolRight, bool isToolLeft, bool isToolDown, bool isToolUp)
{// Get grid property detailsGridPropertyDetails gridPropertyDetails = GridPropertiesManager.Instance.GetGridPropertyDetails(cropGridPosition.x, cropGridPosition.y);if (gridPropertyDetails == null)return;// Get seed item detailsItemDetails seedItemDetails = InventoryManager.Instance.GetItemDetails(gridPropertyDetails.seedItemCode);if(seedItemDetails == null) return;// Get crop detailsCropDetails cropDetails = GridPropertiesManager.Instance.GetCropDetails(seedItemDetails.itemCode);if (cropDetails == null) return;// Get animator for crop if presentAnimator animator = GetComponentInChildren<Animator>();// Trigger tool animationif(animator != null){if(isToolRight || isToolUp){animator.SetTrigger("usetoolright");}else if(isToolLeft || isToolDown){animator.SetTrigger("usetoolleft");}}// Trigger tool particle effect on cropif (cropDetails.isHarvestActionEffect){EventHandler.CallHarvestActionEffectEvent(harvestActionEffectTransform.position, cropDetails.harvestActionEffect);}// Get required harvest actions for tool(收获此农作物所需的操作次数)int requiredHarvestActions = cropDetails.RequiredHarvestActionsForTool(equippedItemDetails.itemCode);if (requiredHarvestActions == -1) return;// Increment harvest action countharvestActionCount++;// Check if required harvest actions madeif (harvestActionCount >= requiredHarvestActions)HarvestCrop(isToolRight, isToolUp, cropDetails, gridPropertyDetails, animator);}

4、修改CropTreeCanyonOak预设体

在CropSprite下新增一个物体命名为LeavesSpawnPoint,这个作为特效的参考点,修改其Position(Y)为4。

接着点击CropTreeCanyonOak进行变量赋值。

5、修改PoolManager对象

6、修改VFXManager.cs脚本及配置

添加定义:

 [SerializeField] private GameObject deciduousLeavesFallingPrefab = null;

在displayHarvestActionEffect中添加case信息如下:

case HarvestActionEffect.deciduousLeavesFalling:GameObject deciduousLeavesFalling = PoolManager.Instance.ReuseObject(deciduousLeavesFallingPrefab, effectPosition, Quaternion.identity);deciduousLeavesFalling.SetActive(true);StartCoroutine(DisableHarvestActionEffect(deciduousLeavesFalling, twoSeconds));break;

完整的代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class VFXManager : SingletonMonobehaviour<VFXManager>
{private WaitForSeconds twoSeconds;[SerializeField] private GameObject reapingPrefab = null;[SerializeField] private GameObject deciduousLeavesFallingPrefab = null;protected override void Awake(){base.Awake();twoSeconds = new WaitForSeconds(2f);}private void OnDisable(){EventHandler.HarvestActionEffectEvent -= displayHarvestActionEffect;}private void OnEnable(){EventHandler.HarvestActionEffectEvent += displayHarvestActionEffect;}private IEnumerator DisableHarvestActionEffect(GameObject effectGameObject, WaitForSeconds secondsToWait){yield return secondsToWait;effectGameObject.SetActive(false);}private void displayHarvestActionEffect(Vector3 effectPosition, HarvestActionEffect harvestActionEffect){switch(harvestActionEffect){case HarvestActionEffect.deciduousLeavesFalling:GameObject deciduousLeavesFalling = PoolManager.Instance.ReuseObject(deciduousLeavesFallingPrefab, effectPosition, Quaternion.identity);deciduousLeavesFalling.SetActive(true);StartCoroutine(DisableHarvestActionEffect(deciduousLeavesFalling, twoSeconds));break;case HarvestActionEffect.reaping:GameObject reaping = PoolManager.Instance.ReuseObject(reapingPrefab, effectPosition, Quaternion.identity);reaping.SetActive(true); StartCoroutine(DisableHarvestActionEffect(reaping, twoSeconds));break;case HarvestActionEffect.none:break;default:break;}}
}

配置VFXManager的信息如下:

7、运行游戏

http://www.lryc.cn/news/2383970.html

相关文章:

  • 第十节第六部分:常见API:DateTimeFormatter、Period、Duration
  • 如何在VSCode中更换默认浏览器:完整指南
  • B2160 病人排队
  • 【机器人】复现 3D-Mem 具身探索和推理 | 3D场景记忆 CVPR 2025
  • 鸿蒙进阶——CMakelist、GN语法简介及三方库通用移植指南
  • CSS-5.1 Transition 过渡
  • TTS:VITS-fast-fine-tuning 快速微调 VITS
  • 从虚拟仿真到行业实训再到具身智能--华清远见嵌入式物联网人工智能全链路教学方案
  • 告别手动绘图!2分钟用 AI 生成波士顿矩阵
  • GraphPad Prism工作表的管理
  • UE 材质几个输出向量节点
  • 【modelscope/huggingface 通过colab将huggingface 模型/数据集/空间转移到 modelscope并下载】
  • 告别静态UI!Guineration用AI打造用户专属动态界面
  • 第六届电子通讯与人工智能国际学术会议(ICECAI 2025)
  • 【C/C++】C++并发编程:std::async与std::thread深度对比
  • 每日算法刷题Day11 5.20:leetcode不定长滑动窗口求最长/最大6道题,结束不定长滑动窗口求最长/最大,用时1h20min
  • STL中的Vector(顺序表)
  • iOS Runtime与RunLoop的对比和使用
  • 解决vscode在任务栏显示白色图标
  • 架构思维:构建高并发扣减服务_分布式无主架构
  • Vue 3 官方 Hooks 的用法与实现原理
  • Vue3 打印表格、Element Plus 打印、前端打印、表格导出打印、打印插件封装、JavaScript 打印、打印预览
  • 湖北理元理律师事务所:专业债务优化如何助力负债者重获生活掌控权
  • RAGFlow知识检索原理解析:混合检索架构与工程实践
  • 5月22总结
  • Java设计模式之桥接模式:从入门到精通
  • uni-app学习笔记九-vue3 v-for指令
  • MAC电脑中右键后复制和拷贝的区别
  • Regmap子系统之六轴传感器驱动-编写icm20607.c驱动
  • 常见高危端口解析:网络安全中的“危险入口”