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)浇水

相关推荐
ujainu5 小时前
Flutter + OpenHarmony 游戏开发进阶:轨迹拖尾特效——透明度渐变与轨迹数组管理
flutter·游戏·openharmony
缺点内向6 小时前
C#编程实战:如何为Word文档添加背景色或背景图片
开发语言·c#·自动化·word·.net
w-白兰地6 小时前
【Addressable远端加载资源】
unity·addressable·资源加载
UI设计兰亭妙微8 小时前
UI 图标设计核心技巧与设计师职业发展指南
ui
GuokLiu8 小时前
260202-OpenWebUI交互式Rich UI嵌入的三种方法-[非交互式]+[静态交互式]+[动态交互式]
ui
学海无涯书山有路9 小时前
async-await异步编程
c#
切糕师学AI9 小时前
ARM 汇编器中的伪指令(Assembler Directives)
开发语言·arm开发·c#
小张不爱写代码9 小时前
[Unity 技巧] 如何自定义 Inspector 变量显示名称 (CustomLabel)
unity·游戏引擎
我的offer在哪里9 小时前
开源 AI 生成游戏平台:原理、开源项目与落地实战指南
人工智能·游戏·开源
Sator110 小时前
Unity开发中常用的随机方法扩展
unity