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控制器死掉。为了您的帮助,我也写了评论,以便大家都能理解。
答案 0 :(得分:1)
我假设您希望敌人路径查找玩家。
如果您需要更多帮助,请阅读此内容: Unity Topics: Navigation
首先,您希望敌人走在的所有对象必须被选为&#34; static &#34;。 (单击GameObject并勾选右上角的静态&#39;
现在您需要转到 Windows&gt;导航并点击&#34; Bake&#34;一旦你喜欢你的设置,就在底部。
确保你的&#34; Enemy&#34;它们上面有一个刚体,然后添加&#34; Nav Mesh Agent&#34; 组件。
现在添加(或编辑)敌人的脚本并添加代码(确保将其添加到顶部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);
}
}
}