發表文章

Related Posts Plugin for WordPress, Blogger...

3-5 Bridges And Navmesh

圖片
大家是否曾有遇過放置橋樑在場景中,卻因為橋樑的設置中是有洞的那種,導致Navmesh運作不正常的情況嗎?今天就要來說明並解決這種情況。 首先如下圖,我用的橋樑物件中間,設計上因為是木頭跟木頭連結,理所當然地應該要有空隙對吧,但是這種空隙卻造成Navmesh計算時也跟著出現空隙。 解決方式就是在空隙的部分插入其他的方塊,我這邊單純新增了一個Cube拉進去,然後調整成空隙的大小後再放進去。我想有些人看到這邊應該會感到困惑,既然要把空隙填起來那為什麼不新增一個大的Cube填起來就好,這樣一個一個填很麻煩。可惜原因就是因為我的橋是拱橋................ 然後記得這個Cube要在Navigation/Object視窗中,將Navigation Static勾起來,Navigation Area選擇Walkable。 按下Bake按鈕。 就會發現有Cube填起來的地方,Navmesh可以正常計算到了!但是填入這樣一個方塊,實在很破壞我們漂亮的拱橋耶!(ˋ0ˊ) 所以,接著我們在Cube的Mesh Renderer中設定Size,原本預設為1。 將Size改成0以後,Cube就看不到啦!Navmesh也仍舊運作正常。 好的,我接著將有需要填滿的空隙都填好了,也將Materials的Size都改成0以確保不會破壞拱橋的美觀。 大家可以看見如下圖,還未重新Bake的Navmesh狀態。 重新Bake以後,我們的拱橋總算是可以行走了,而且填入的Cube也有成功隱藏!

3-4 Changing Your Skybox

圖片
今天要來介紹如何更換你的Skybox,並給場景帶來夜晚的感覺。如果大家要上網找教學的話,也要注意嘍!Unity 5以上的Skybox設定方式跟以前不同,所以有可能會找到舊版的教學。 首先,先在Project視窗點右鍵,Create/Material。 將Material取名為Red Sky Box,Shader選擇Skybox/Procedural,這是Unity提供的使用程序產生的Skybox,只要調整顏色跟參數就會有不同的效果。當然,還有其他用貼圖類型的Skybox,這部分後面再講。 然後開啟Window/Lighting/Settings。 將我們剛剛新增的Red Sky Box拉進Environment/Skybox Material中。 接下來,大家就可以回到Red Sky Box裡面調整設定和顏色了,會有很多不同的效果唷! 比方說我調整成紅色,天空就會變成如下圖那樣,有點紫紫的,大概就是原本天空的藍色再混合紅色,就變成類似紫色的感覺了吧。 然後我們再根據紫色的Skybox調整光線,回到Lighting視窗,將Environment/Environment Lighting/Source的設定調整成Color。 選擇橘色的話,場景的各個物件打光顏色就會偏橘色。 選擇藍綠色的話,也就會偏藍綠色。 接著,請大家在Asset Store下載免費的Skybox資源: https://www.assetstore.unity3d.com/en/#!/content/18353 然後如下圖僅匯入需要的內容。 當大家開啟Fantasy Skybox FREE/Materials資料夾後,會看見這個免費資源提供了四種Skybox。首先我把Night 01B拉進場景的天空中。 Skybox立即就變成這個Material了,馬上就有夜晚的感覺了呢! 大家可以從Inspector視窗中看見,這種Skybox就是各種不同方向的貼圖拼裝出來的。 然後回到Lighting視窗,將Environment/Sun Source設定成我們場景中的Sun。 然後再將Sun的光線顏色設定成如下圖那樣的淺藍色。 最後,整個遊戲場景就會有晚上的感覺了!

3-3 Trigger Player Hit Animation And Audio On Radius

圖片
本章要介紹如何播放Player的攻擊動畫,以及當Player進入到某個範圍後會播放音效。因為前半段製作過程都已有文章進行說明,如大家有不懂的地方請先閱讀以下文章: 2-27 Import Mechanim Animation Pack https://rpgcorecombat.blogspot.tw/2017/12/2-27-import-mechanim-animation-pack.html 3-2 The Animator Override Controller https://rpgcorecombat.blogspot.tw/2017/12/3-2-animator-override-controller.html 主要更改的程式碼為Player.cs,只要在呼叫IDamageable的TakeDamage之前,使用animator呼叫SetTrigger方法即可,如下圖。 我在Player.cs中進行了一些重構讓方法可以自我解釋: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Assertions; using RPG.CameraUI; // TODO consider re-wiring using RPG.Core; using RPG.Weapons; namespace RPG.Character{ public class Player : MonoBehaviour, IDamageable { [SerializeField] float maxHealthPoints = 100f; [SerializeField] float damagePerHit = 50f; [SerializeField] AnimatorOverrideController animatorOverrideController; [SerializeField] Weapon weaponInUse; Animator animator; float currentHealthPoint; float lastHitTime; ...

3-2 The Animator Override Controller

圖片
本章要讓Player使用不同的武器時會播放不同的Animation,使用的方式為Animator Override Controller,先來引用官方的介紹: https://docs.unity3d.com/Manual/AnimatorOverrideController.html The Animator Override Controller is a type of asset which allows you to extend an existing Animator Controller, replacing the specific animations used but otherwise retaining the original’s structure, parameters and logic. This allows you to create multiple variants of the same basic state machine, but with each using different sets of animations. For example, your game may have a variety of NPC types living in the world, but each type (goblin, ogre, elf, etc) has their own unique animations for walking, idling, sitting, etc. By creating one “base” Animator Controller containing the logic for all NPC types, you could then create an override for each type and drop in their respective animation files. 簡單說明上述的介紹,Animator Override Controller能在不改變原有State Machine框架下,動態變更Animation clip。比方說,你的遊戲可能有不同類型的NPC都套用相同的Animator,亦即套用相同的走路、待命、坐下等State M...