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

相关推荐
霜绛24 分钟前
Unity:UGUI笔记(一)——三大基础控件、组合控件
笔记·学习·unity·游戏引擎
YuanlongWang1 小时前
c# 泛型的详细介绍
c#
麦麦大数据3 小时前
F029 vue游戏推荐大数据可视化系统vue+flask+mysql|steam游戏平台可视化
vue.js·游戏·信息可视化·flask·推荐算法·游戏推荐
嵌入式学习和实践3 小时前
C# WinForms 多窗口交互通信的示例-主窗口子窗口交互通信
c#·交互·主窗口-子窗口通信
专注VB编程开发20年3 小时前
C#,VB.NET数组去重复,提取键名和重复键和非重复键
c#·.net·linq·取唯一键·去重复·重复数量
YuanlongWang4 小时前
Entity Framework Core和SqlSugar的区别,详细介绍
c#
小趴菜82276 小时前
Android中加载unity aar包实现方案
android·unity·游戏引擎
unicrom_深圳市由你创科技7 小时前
工业上位机,用Python+Qt还是C#+WPF?
python·qt·c#
知识分享小能手13 小时前
uni-app 入门学习教程,从入门到精通,uni-app基础扩展 —— 详细知识点与案例(3)
vue.js·学习·ui·微信小程序·小程序·uni-app·编程
今夕资源网14 小时前
牛童三国单机游戏Unity源码 免费开源
游戏·unity·单机游戏·游戏源码·unity源码·unity游戏