Unity3D仿星露谷物语开发69之动作声音

1、目标

Player动作时产生的声音,比如砍倒树木、砸石头。

2、修复NPC快速行进的bug(与本节无关)

修改NPCMovement.cs脚本的MoveToGridPositionRoutine方法。

确保npcCalculatedSpeed的速度不少于最慢速度。

原代码:

修改后的代码:

3、修改动作声音相关的代码及对象

(1)修改CropDetails.cs脚本

添加一行代码:

(2)修改so_CropDetailsList的实例

位于:Assets/Scriptable Object Assets/Crop/so_CropDetailsList.asset

所有的配置如下:

|--------|---------------|-----------------------|
| 序号 | Item Code | Harvest Sound |
| 0 | 10006 | Effect Pluck |
| 1 | 10000 | Effect Tree Falling |
| 2 | 10010 | Effect Wood Splinters |
| 3 | 10009 | Effect Tree Falling |
| 4 | 10011 | Effect Wood Splinters |
| 5 | 10014 | Effect Stone Shatter |
| 6 | 10016 | Effect Stone Shatter |

(3)修改Crop.cs脚本

修改HarvestCrop函数添加如下代码:

完整代码如下:

cs 复制代码
 private void HarvestCrop(bool isUsingToolRight, bool isUsingToolUp, CropDetails cropDetails, GridPropertyDetails gridPropertyDetails, Animator animator)
 {
     // Is there a harvested animation
     if(cropDetails.isHarvestedAnimation && animator != null)
     {
         // if harvest sprite then add to sprite renderer
         if(cropDetails.harvestedSprite != null)
         {
             if(cropHarvestedSpriteRenderer != null)
             {
                 cropHarvestedSpriteRenderer.sprite = cropDetails.harvestedSprite;  // 一张图片
             }
         }

         if(isUsingToolRight || isUsingToolUp)
         {
             animator.SetTrigger("harvestright");
         }
         else
         {
             animator.SetTrigger("harvestleft");
         }
     }

     // Is there a harvested sound
     if(cropDetails.harvestSound != SoundName.none)
     {
         AudioManager.Instance.PlaySound(cropDetails.harvestSound);
     }

     // Delete crop from grid properties
     gridPropertyDetails.seedItemCode = -1;
     gridPropertyDetails.growthDays = -1;
     gridPropertyDetails.daysSinceLastHarvest = -1;
     gridPropertyDetails.daysSinceWatered = -1;

     // Should the crop be hidden before the harvested animation
     if (cropDetails.hideCropBeforeHarvestedAnimation)
     {
         GetComponentInChildren<SpriteRenderer>().enabled = false;
     }

     // Should box colliders be disabled before harvest
     if (cropDetails.disableCropCollidersBeforeHarvestedAnimation)
     {
         // Disable any box colliders
         Collider2D[] collider2Ds = GetComponentsInChildren<Collider2D>();
         foreach(Collider2D collider2D in collider2Ds)
         {
             collider2D.enabled = false;
         }
     }


     GridPropertiesManager.Instance.SetGridPropertyDetails(gridPropertyDetails.gridX, gridPropertyDetails.gridY, gridPropertyDetails);

     // Is there a harvested animation - Destory this crop game object after animation completed
     if(cropDetails.isHarvestedAnimation && animator != null)
     {
         StartCoroutine(ProcessHarvestedActionsAfterAnimation(cropDetails, gridPropertyDetails, animator));
     }
     else
     {
         HarvestActions(cropDetails, gridPropertyDetails);
     }
        
 }

(4)修改ItemNudge.cs脚本

修改OnTriggerEnter2D和OnTriggerExit2D函数添加如下内容:

确保Player的Tag是"Player"。

(5)修改ItemPickup.cs脚本

修改OnTriggerEnter2D函数添加如下内容:

(6)修改Player.cs脚本

修改PlantSeedAtCursor函数添加如下内容:

修改HoeGroundAtCursor函数添加如下代码:

修改WaterGroundAtCursor函数添加如下代码:

修改ChopInPlayerDirection函数添加如下代码:

修改CollectInPlayerDirection函数添加如下代码:

修改BreakInPlayerDirection函数添加如下代码:

修改UseToolInPlayerDirection函数添加如下代码:

运行游戏:

如下情况会有声音:

1)收集道具

2)穿过草丛

3)用镰刀割草

4)用稿子敲击石头

5)挖地

6)播散种子

7)砍树

8)用篮子收集萝卜

9)浇水

相关推荐
小羊没烦恼!4 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
神仙别闹4 天前
基于C#+MySQL实现(WinForm)个人聊天室软件
c#
大蓝头4 天前
安装包UI美化之路-nsNiuniuSkin全新UI调试与预览方法
ui·electron·安装包·截图控件·截屏控件
kybs19914 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
慧都小项5 天前
当边缘AI上产线:QtitanDocking 如何让工业 HMI 实现多视图协同
人工智能·qt·ui·边缘计算·qt6.3
魔士于安5 天前
unity 丑陋哥布林 哥布林有humannoid 动画没 带动画
unity·游戏引擎·材质·贴图·模型
JQweryuiop5 天前
中小型游戏陪练工作室数字化管理实践:基于七彩屋电竞护航小程序订单、履约与绩效系统的落地复盘
大数据·游戏·小程序·架构
Polevne5 天前
C# SFTP客户端实现文件传输
c#·ssh
samble5 天前
从零搭建灌装监控系统(四):深色主题与样式系统,ResourceDictionary 统一管理
c#·wpf·mvvm·样式·工业控制
cjp5605 天前
024 . WPF MVVM类封装优化
c#