0

Im making a wave survival game where you can build up defenses and I want to make it so the enemies will attack the objects you placed down to break them and come attack you. I want to know if theres a way to find if the enemy is not moving at a certain speed, therefore they are stuck behind a wall trying to get to the player. And use that to make them attack the nearest wall if they are stuck.

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);
            }
        }
    }
}

I added the health script to the walls used on the player as well as a rigidbody

derHugo
  • 83,094
  • 9
  • 75
  • 115
Cri
  • 85
  • 4
  • 16

1 Answers1

0

Assuming you have rigidbodies attached to your enemies, you can use the magnitude of the velocity component of the rigidbody to determine its speed. With this you can do something like...

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, 
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
ryeMoss
  • 4,303
  • 1
  • 15
  • 34
  • `magnitude` calls square root function, `sqrMagnitude` doesn't so the latter is faster – trollingchar Apr 02 '19 at 14:28
  • The enemies arent attacking me at all even though there is nothing with the wall tag, after I place down a wall they start moving towards it, but they run into an object on the way and cant seem to get around it, as well as thats its only enemy clones that will move. Would having it so if the enemies velocity is below a certain value for a certain amount of time then attack the wall work? – Cri Apr 03 '19 at 13:46