NavMesh動態碰撞
阿新 • • 發佈:2017-07-25
void tor enter 動態添加 pre .com this div parent
思路來源自http://www.cnblogs.com/sifenkesi/p/4004215.html
今天遇到一個問題,就是怎樣處理一些動態的障礙物。
NavMesh是能夠躲避靜態的障礙物。NavMeshObstacle的作用就是動態添加障礙。
可是有個問題,NavMeshObstacle是圓,連橢圓都不行,所以。僅僅好寫一個附屬腳本。用圓拼成矩形,就能夠了。
using UnityEngine; using System.Collections; public class NavMeshObstacleHelper : MonoBehaviour { //coordinate public float X = 0f; public float Y = 0f; public float Z = 0f; public float Length = 0f; public float Width = 0f; public float Height = 0f; public float Diameter = 0f; private int lengthCount = 0; private float lengthStep = 0f; private int widthCount = 0; private float widthStep = 0f; private GameObject obstacleArray = null; private GameObject obstacle = null; void Awake() { obstacleArray = new GameObject (); obstacleArray.name = "NavMeshObstacleArray"; widthCount = (int)(Width / Diameter); lengthCount = (int) (Length / Diameter); if (lengthCount > 1) { lengthStep = (Length - Diameter * lengthCount) / (lengthCount - 1); } if (widthCount > 1) { widthStep = (Width - Diameter * widthCount) / (widthCount - 1); } } // Use this for initialization void Start () { initObstacleArray (); } private void initObstacleArray() { Vector3 tempPos = new Vector3 (X, Y, Z); for (int i = 0; i < lengthCount; i++) { for (int j = 0; j < widthCount; j++) { obstacle = new GameObject (); obstacle.transform.position = tempPos; obstacle.transform.parent = obstacleArray.transform; obstacle.AddComponent <NavMeshObstacle>(); NavMeshObstacle navMeshObstacle = obstacle.GetComponent<NavMeshObstacle> (); if (navMeshObstacle) { obstacle.GetComponent<NavMeshObstacle> ().radius = Diameter / 2; obstacle.GetComponent<NavMeshObstacle> ().height = Height; } tempPos = new Vector3 (tempPos.x, tempPos.y, tempPos.z + Diameter + widthStep); } tempPos = new Vector3 (tempPos.x + Diameter + lengthStep, tempPos.y, Z); } obstacleArray.transform.parent = this.transform; obstacleArray.transform.localRotation = Quaternion.identity; obstacleArray.transform.position = this.transform.position; } // Update is called once per frame void Update () { } }
思路來源自http://www.cnblogs.com/sifenkesi/p/4004215.html
能夠看一下。效果:
參數設置
NavMesh動態碰撞