0

我正在制作一个波浪生存游戏,您可以在其中建立防御,我想制作它,以便敌人会攻击您放置的物体以破坏它们并攻击您。我想知道是否有办法找到敌人是否没有以一定的速度移动,因此他们被困在墙后面试图接近玩家。如果他们被卡住,用它来让他们攻击最近的墙壁。

using UnityEngine;
using System.Collections;

namespace CompleteProject
{
    public class EnemyMovement : MonoBehaviour
    {
        // Reference to the player's position.
        Transform player;               
        Transform wall;

        // Reference to the player's health.
        PlayerHealth playerHealth;  

        // Reference to this enemy's health.    
        EnemyHealth enemyHealth;  

        // Reference to the nav mesh agent.      
        UnityEngine.AI.NavMeshAgent nav;               

        void Awake ()
        {
            // Set up the references.
            player = GameObject.FindGameObjectWithTag ("Player").transform;
            playerHealth = player.GetComponent <PlayerHealth> ();
            enemyHealth = GetComponent <EnemyHealth> ();
            nav = GetComponent <UnityEngine.AI.NavMeshAgent> ();
            wall = GameObject.FindGameObjectWithTag("Wall").transform;
        }

        void Update ()
        {
            // If the enemy and the player have health left...
            if(enemyHealth.currentHealth > 0 && playerHealth.currentHealth > 0)
            {
                // ... set the destination of the nav mesh agent to the player.
                nav.SetDestination(player.position);
            }
            // Otherwise...
            else
            {
                // ... disable the nav mesh agent.
                nav.SetDestination(player.position);
            }
        }
    }
}

我将健康脚本添加到玩家使用的墙壁以及刚体

4

1 回答 1

0

假设你有刚体连接到你的敌人,你可以使用刚体的速度分量的大小来确定它的速度。有了这个,你可以做类似的事情......

Rigidbody2D rbody;

void Start()
{
    rbody = GetComponent<Rigidybody2D>();
}

void Update()
{
    if (rbody.velocity.magnitude < 1f)
    {
        // change target to wall or do some logic here
    }
    else
    {
       // target is player, 
    }
}
于 2019-04-02T14:13:21.573 回答