5.10導航系統

weixin_34185364發表於2017-05-10

Nav Mesh Agent 自動尋路(新增在移動的物體上)

Off Mesh Link分離路面(新增在跳躍的起始點的其中一個點上)

Nav Mesh Obstacle動態障礙(新增在障礙物上)

運動的物體都不能設定為Static

usingUnityEngine;

usingSystem.Collections;

publicclassNavigationScript:MonoBehaviour{

private NavMeshAgent  agent;

public  Transform[ ]  targetTransform;

float time;

private  int  TargetIndex=1;

voidStart( ){

agent=GetComponent<NavMeshAgent>( );

}

voidUpdate( ){

//走到滑鼠點選的位置

//if(Input.GetMouseButtonDown(0)){

//  agent.destination = targetTransform.position;


物體移動到滑鼠點選的位置

//   RaycastHit   hit;

//if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition),out  hit)){

//if(hit.collider.name.Contains("Plane")){

////設定目標點

//agent.destination=hit.point;

//}

//}

//}

//if(Input.GetKeyDown(KeyCode.Alpha1)){

//agent.areaMask=9;

//GameObject.Find("Road2").SetActive(false);

//}

//if(Input.GetKeyDown(KeyCode.Alpha2)){

//agent.areaMask=17;

//GameObject.Find("Road1").SetActive(false);

//}



//巡邏

if(agent.remainingDistance==0){

time+=Time.deltaTime;

if(time>=3.0f){

TargetIndex++;

//目標點迴圈更改

TargetIndex=TargetIndex%targetTransform.Length;

//設定目標點

agent.destination=targetTransform[TargetIndex].position;

time=0.0f;

}

}else{

agent.destination=targetTransform[TargetIndex].position;

}

//if(Input.GetMouseButtonDown(0)){

//if(gameObject.name=="Player1"){

//agent.destination=targetTransform[1].position;

//}else{

//agent.destination=targetTransform[0].position;

//}

//}

}

}

相關文章