發表文章

目前顯示的是 10月, 2019的文章

[Unity] RPG 想隨機生怪,又不想怪生在石頭上 水中 等卡點的地方解決辦法 NavMesh.SamplePosition

想要在一整片開放地圖,隨機生怪,又不想用點位,一一設定的話,可以用NavMesh.SamplePosition 但是先決條件是你的地圖是透過 NavMesh 去Bake路線的, 才能夠使用這個辦法 # NavMesh  參考資料 :  https://docs.unity3d.com/ScriptReference/AI.NavMesh.html                                         https://jerrard-liu.blogspot.com/2016/07/NavMeshBaking.html 如果要在可走的地方隨機生怪可以用以下方法 public struct OptPos { public bool ok ; public Vector3 pos ; } private OptPos randomOptPos ( Vector3 pos) { NavMeshHit hit ; if ( NavMesh . SamplePosition ( pos , out hit , 1.0 f , NavMesh . AllAreas ) ) { return new OptPos ( ) { ok = true , pos = hit . position } ; } return new OptPos ( ) { pos = pos } ; } unity3d   帶入pos (隨機的點位),他會搜尋範圍 " 1.0 f " 可以放的點位,如果沒有則失敗,所以數值愈大 找的範圍越大 參考文獻: https://docs.unity3d.com/ScriptRefe