第九周1

准备连线的时候补充了一下 追击到走路的Transfer的返回概率函数

因为之前还是不太了解改如何解决AI 碰到Player躲藏在障碍物的状况,按理来说应该是不再追击(或者是提高不再追击的概率)

更改状态为在附近走动。

选择用射线模拟扇形的方式做AI的观测方向

原理是这样的


射线检测的方式如下代码:


(以上代码和图片都是找的资料)

因此AI中的转换节点更改成:

	//追击转化为走路
	public static float Transfer_TraceToWalk(AIEntity pEntity){
		float dis = Vector3.Distance (pEntity.AIPos, pEntity.PlayerPos);
		float  angle=Vector3.Dot((pEntity.PlayerPos-pEntity.AIPos).normalized,pEntity.GetComponent<AIMove>().mDirection);
		//当距离远离一定程度或者看不到的时候,停止追击

		RaycastHit hit;
		bool outWall = true;
		float subAngle = (120 / 2) / 10;//10是精度
	//或者视线被某个东西挡住了,停止追击换成巡逻
		if (dis > 80.0f) {
			return 0.65f;
		} else {
			if (angle <= 0.3) {
				return dis / 80.0f * pEntity.GetComponent<myBossAI> ().tired / pEntity.GetComponent<myBossAI> ().tiredAll * 10.0f;
			} else {
				//在距离范围内,在视线范围内
				for(int i=0;i<10;i++){
					Ray ray=new Ray(pEntity.AIPos,Quaternion.Euler(0,subAngle*i,0) *pEntity.GetComponent<AIMove>().mDirection);
					if(Physics.Raycast(ray,out hit,50.0f)){
						if (!hit.collider.CompareTag ("Wall")) {
							outWall = false;
							break;
						} 
				}
					if (outWall) {
					//被墙挡住了,概率加大
						return dis/50.0f;
					}
					else {
						//没被墙挡住,概率减小
						return dis/50.0f/3.0f;
					}
				}
			}
		}

	}

猜你喜欢

转载自blog.csdn.net/vancooler/article/details/80687259