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

相关推荐
hez20103 天前
在 .NET 上构建超大托管数组
c#·.net·.net core·gc·clr
金銀銅鐵5 天前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏
金銀銅鐵6 天前
借助 Pygame 探索最大公约数的规律
python·数学·游戏
雨落倾城夏未凉8 天前
第四章c#方法-参数数组和可选参数(16)
后端·c#
唐青枫9 天前
线程不是越多越快:C#.NET Thread 生命周期、同步与后台工作线程实战
c#·.net
唐青枫10 天前
别只会反射:C#.NET Emit 动态生成代码实战详解
c#·.net
咕白m62510 天前
.NET 环境下 Word 超链接批量提取方案
c#·.net
用户917215619021110 天前
C# 通信协议增量解析:用状态机处理半包和粘包
c#
nujnewnehc10 天前
不会 py, 用 ai 写了个游戏辅助的感受
人工智能·游戏
小码编匠11 天前
C# 工控上位机必备:数据转换工具类与十个核心模块
后端·c#·.net