2017-03-13 56 views
0
using UnityEngine; 
using System.Collections; 

public class EnemyAttack : MonoBehaviour 
{ 
    public float timeBetweenAttacks = 0.5f;  // The time in seconds between each attack. 
    public int attackDamage = 10;    // The amount of health taken away per attack. 

    Animator anim;        // Reference to the animator component. 
    GameObject player;       // Reference to the player GameObject. 
    PlayerHealth playerHealth;     // Reference to the player's health. 
    //EnemyHealth enemyHealth;     // Reference to this enemy's health. 
    bool playerInRange;       // Whether player is within the trigger collider and can be attacked. 
float timer;        // Timer for counting up to the next attack. 

void Awake() 
{ 
    // Setting up the references. 
    player = GameObject.FindGameObjectWithTag ("Player"); 
    playerHealth = player.GetComponent <PlayerHealth>(); 
    //enemyHealth = GetComponent<EnemyHealth>(); 
    anim = GetComponent <Animator>(); 
} 

public void OnTriggerEnter (Collider other) 
{ 
    // If the entering collider is the player... 
    if(other.gameObject == player) 
    { 
     // ... the player is in range. 
     playerInRange = true; 
     anim = ("idle0ToAttack1"); 
    } 
} 

void OnTriggerExit (Collider other) 
{ 
    // If the exiting collider is the player... 
    if(other.gameObject == player) 
    { 
     // ... the player is no longer in range. 
     playerInRange = false; 
    } 
} 


void Update() 
{ 
    // Add the time since Update was last called to the timer. 
    timer += Time.deltaTime; 

    // If the timer exceeds the time between attacks, the player is in range     and this enemy is alive... 
    if(timer >= timeBetweenAttacks && playerInRange /*&& enemyHealth.currentHealth > 0*/) 
    { 
     // ... attack. 
     Attack(); 
    } 

    // If the player has zero or less health... 
    if(playerHealth.currentHealth <= 0) 
    { 
     // ... tell the animator the player is dead. 
     Destroy(this.gameObject); 
    } 
} 


void Attack() 
{ 
    // Reset the timer. 
    timer = 0f; 

    // If the player has health to lose... 
    if(playerHealth.currentHealth > 0) 
    { 
     // ... damage the player. 
     playerHealth.TakeDamage (attackDamage); 
    } 
    } 
} 

我想讓敵方攻擊fps玩家。我怎麼做?同時我希望我的fps控制器死亡。爲了您的幫助,我還寫了評論,以便大家都能理解。獲得對fps控制器的敵人攻擊?

+0

看起來相當部分的援助之下呈現,Durvesh,但它完全滑落你的頭腦以某種方式作出迴應。要回復帖子,您可以發表評論,贊成或接受。如果你沒有做任何這些事情,幫手可能會變得士氣低落,並決定幫助人們浪費他們的時間。那麼,你現在會迴應他們嗎? – halfer

回答

1

我假設你想讓你的敵人路徑查找的玩家。

閱讀這個,如果你需要更多的幫助:Unity Topics: Navigation

第一步,你要爲你的敵人步行路程所有對象必須選擇「靜態」。 (點擊遊戲對象並點擊右上角的'靜態')

現在你需要去Windows>導航然後點擊下面的「烘烤」一旦你喜歡你的設置。

確保你對它們的「敵人」有剛體,然後添加「導航網狀代理」組件。

現在添加(或編輯)的腳本敵人,並添加代碼(確保添加這件事上using UnityEngine.AI;):

//NavMeshAgent 
private NavMeshAgent agent; 

//Target Transform 
public Transform trans; 

void Start() 
{ 
    //Get the component 
    agent = GetComponent<NavMeshAgent>(); 

} 

private void Update() 
{ 
    SetDestination(trans.position); 
} 

void SetDestination (Vector3 des) 
{ 
    //Set the destination 
    agent.SetDestination(des); 
} 

而且你去那裏,也有這樣做的其他方法但這是迄今爲止最簡單的。

編輯:

這是你的腳本應該是什麼樣子:

using UnityEngine; 
using System.Collections; 

using UnityEngine.AI; 

public class EnemyAttack : MonoBehaviour 
{ 
    public float timeBetweenAttacks = 0.5f;  // The time in seconds between each attack. 
    public int attackDamage = 10;    // The amount of health taken away per attack. 

    Animator anim;        // Reference to the animator component. 
    GameObject player;       // Reference to the player GameObject. 
    PlayerHealth playerHealth;     // Reference to the player's health. 
    //EnemyHealth enemyHealth;     // Reference to this enemy's health. 
    bool playerInRange;       // Whether player is within the trigger collider and can be attacked. 
    float timer;        // Timer for counting up to the next attack. 

    //NavMeshAgent 
    private NavMeshAgent agent; 

    //Target Transform 
    public Transform trans; 

    void Start() 
    { 
     //Get the component 
     agent = GetComponent<NavMeshAgent>(); 

    } 
    void SetDestination(Vector3 des) 
    { 
     //Set the destination 
     agent.SetDestination(des); 
    } 

    void Awake() 
    { 
     // Setting up the references. 
     player = GameObject.FindGameObjectWithTag("Player"); 
     playerHealth = player.GetComponent<PlayerHealth>(); 
     //enemyHealth = GetComponent<EnemyHealth>(); 
     anim = GetComponent<Animator>(); 
    } 


    public void OnTriggerEnter(Collider other) 
    { 
     // If the entering collider is the player... 
     if (other.gameObject == player) 
     { 
      // ... the player is in range. 
      playerInRange = true; 
      anim = ("idle0ToAttack1"); 
     } 
    } 

    void OnTriggerExit(Collider other) 
    { 
     // If the exiting collider is the player... 
     if (other.gameObject == player) 
     { 
      // ... the player is no longer in range. 
      playerInRange = false; 
     } 
    } 


    void Update() 
    { 
     SetDestination(trans.position); 

     // Add the time since Update was last called to the timer. 
     timer += Time.deltaTime; 

     // If the timer exceeds the time between attacks, the player is in range     and this enemy is alive... 
     if (timer >= timeBetweenAttacks && playerInRange /*&& enemyHealth.currentHealth > 0*/) 
     { 
      // ... attack. 
      Attack(); 
     } 

     // If the player has zero or less health... 
     if (playerHealth.currentHealth <= 0) 
     { 
      // ... tell the animator the player is dead. 
      Destroy(this.gameObject); 
     } 
    } 


    void Attack() 
    { 
     // Reset the timer. 
     timer = 0f; 

     // If the player has health to lose... 


    if (playerHealth.currentHealth > 0) 
    { 
     // ... damage the player. 
     playerHealth.TakeDamage(attackDamage); 
    } 
} 
}