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

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、运行游戏

相关文章:

  • 四元数中 w xyz 的含义及应用
  • 一文掌握 LoRA 常见变体
  • Ubuntu操作系统里面安装Docker Compose
  • 大模型高效微调技术全面解析:从PEFT原理到实战应用
  • 简单理解 LightRAG
  • 力扣-快乐数
  • leetcode hot100刷题日记——6.和为 K 的子数组
  • 【Java微服务组件】异步通信P1—消息队列基本概念
  • 【Linux】第二十三章 控制启动过程
  • 高版本Fastjson:Getter调用限制及绕过方式探究
  • python学习day3
  • Linux 磁盘扩容实战案例:从问题发现到完美解决
  • idea部署本地仓库和连接放送远程仓库
  • 2025年PMP 学习二十三 16章 高级项目管理
  • IGBT选型时需关注的参数,适用场景(高压大电流低频)以及驱动电路设计注意事项
  • 【信息系统项目管理师】第11章:项目成本管理 - 32个经典题目及详解
  • 六足连杆爬行机器人的simulink建模与仿真
  • ISCC 2025决赛 wp
  • HBCPC2025 补题 (F、I)
  • 家用和类似用途电器的安全 第1部分:通用要求 与2005版差异(6)
  • 姜再冬会见巴基斯坦空军参谋长:中方欢迎并支持巴印通过对话妥善处理分歧
  • 巴基斯坦西南部一辆客车遭袭造成至少4死30伤
  • 全国首例闭环脊髓神经接口手术在浙江完成,截瘫患者实现自主行走
  • 人民日报评论员观察:稳企业,全力以赴纾困解难
  • 全总联合六部门印发工作指引,共保劳动者合法权益
  • 减负举措如何助力基层干部轻装上阵?记者一线调查