2017-04-21 27 views
1

我試圖創建一個流浪AI流浪AI團結C#

我使用統一標準的資產第三人正在AI

但問題是AI只移動到某一點,它不能

這些點之間巡邏

這裏是代碼?

我該如何修改它巡邏?

 
using System; 
using UnityEngine; 

namespace UnityStandardAssets.Characters.ThirdPerson 
{ 
    [RequireComponent(typeof (UnityEngine.AI.NavMeshAgent))] 
    [RequireComponent(typeof (ThirdPersonCharacter))] 
    public class AICharacterControl : MonoBehaviour 
    { 
     public UnityEngine.AI.NavMeshAgent agent { get; private set; }    // the navmesh agent required for the path finding 
     public ThirdPersonCharacter character { get; private set; } // the character we are controlling 
     public Transform target;         // target to aim for 


     private void Start() 
     { 
      // get the components on the object we need (should not be null due to require component so no need to check) 
      agent = GetComponentInChildren(); 
      character = GetComponent(); 

      agent.updateRotation = false; 
      agent.updatePosition = true; 
     } 


     private void Update() 
     { 
      if (target != null) 
       agent.SetDestination(target.position); 

      if (agent.remainingDistance > agent.stoppingDistance) 
       character.Move(agent.desiredVelocity, false, false); 
      else 
       character.Move(Vector3.zero, false, false); 
     } 


     public void SetTarget(Transform target) 
     { 
      this.target = target; 
     } 
    } 
} 

回答

2

爲了使兩點之間的AI巡邏,你需要定義第二個點,並改變了AI的行爲改變目標時,得到的第一個點。目前,一旦達到其目標(即停止),它將簡單地以零速度移動。

沒有太多修改你的代碼,你可以通過做這樣的事情來擴展它在兩個位置之間移動。

using System; 
using UnityEngine; 

namespace UnityStandardAssets.Characters.ThirdPerson 
{ 

    [RequireComponent(typeof (UnityEngine.AI.NavMeshAgent))] 
    [RequireComponent(typeof (ThirdPersonCharacter))] 
    public class AICharacterControl : MonoBehaviour 
    { 
     public UnityEngine.AI.NavMeshAgent agent { get; private set; }    // the navmesh agent required for the path finding 
     public ThirdPersonCharacter character { get; private set; } // the character we are controlling 
     public Transform start; 
     public Transform end; 

     private Transform target; 
     private boolean forward = true; 

     private void Start() 
     { 
      // get the components on the object we need (should not be null due to require component so no need to check) 
      agent = GetComponentInChildren<UnityEngine.AI.NavMeshAgent>(); 
      character = GetComponent<ThirdPersonCharacter>(); 

      agent.updateRotation = false; 
      agent.updatePosition = true; 
     } 


     private void Update() 
     { 
      if (target != null) 
       agent.SetDestination(target.position); 

      if (agent.remainingDistance > agent.stoppingDistance) 
      { 
       character.Move(agent.desiredVelocity, false, false); 
      } 
      else 
      { 
       SetTarget(forward ? start : end); 
       forward = !forward; 
      } 
     } 

     public void SetTarget(Transform target) 
     { 
      this.target = target; 
     } 
    } 
} 

正如你所看到的,我已經修改了Update()告訴AI改變目標,如果它變得太接近目前的目標。頂部還有一個需要設置的額外變換定義(start),以及一個用於存儲AI前進方向的boolean

此代碼尚未在Unity中測試過,因此可能需要一些修改,但它應該給你正確的想法。

+0

非常感謝兄弟 – Arash