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

相关推荐
加号34 小时前
【C#】 串口通信技术深度解析及实现
开发语言·c#
无风听海6 小时前
C# 隐式转换深度解析
java·开发语言·c#
涵涵(互关)6 小时前
Naive-ui树型选择器只显示根节点
前端·ui·vue
LateFrames6 小时前
520 - 如何说晚安 (WPF)
c#·wpf·浪漫·ui体验
richard_yuu7 小时前
鸿蒙治愈游戏模块实战|四大轻量解压游戏、ArkTS动画交互与低功耗落地
游戏·交互·harmonyos
魔法阵维护师7 小时前
从零开发游戏需要学习的c#模块,第十四章(保存和加载)
学习·游戏·c#
测试员周周9 小时前
【Appium 系列】第13节-混合测试执行器 — API + UI 的协同执行
开发语言·人工智能·python·功能测试·ui·appium·pytest
莽夫搞战术9 小时前
【Google Stitch】AI原生画布重新定义设计,让想法变成可交互界面
前端·人工智能·ui
2301_7807896611 小时前
手游遇到攻击为什么要用SDK游戏盾手游遇到攻击为什么要用 SDK 游戏盾?
安全·web安全·游戏·架构·kubernetes·ddos
Xin_ye1008611 小时前
C# 零基础到精通教程 - 第十一章:LINQ——语言集成查询
开发语言·c#