using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyMove : MonoBehaviour { /*Add the following elements to the inspector: * Rigidbody 2d: freeze rotation in the z axis * Boxcollider 2d: Make the size of the enemy * Second Boxcollider 2d: make very thin at the bottom of the Enemy and set isTrigger * add this Enemy Move Script * Set all values in the script, make sure that you add the player from the level, and the bullet from the prefabs */ //Set the following values in the inspector! //public variable for the player object(use the player object on the scene NOT the prefab) public GameObject player; //Variable for Enemy Health public int EnemyHealth = 5; //public variable for the bullet(use the prefab) public GameObject bulletPrefab; //Speed of bullet public int bulletSpeed = 200; //speed of enemy object public float moveSpeed = 5f; //variable for the rigidbody of the enemy and the bullet Rigidbody2D rb; //global variable for the rotation of the enemy object Quaternion rotation; //boolean to see if player is in range of enemy bool inRange = false; //float for the distance the player is away from //the enemy objects, set in inspector public float enemyRange = 5; //timer for shoot delay. float timer = 0f; //Amount of delay before enemy repeats its shots. public float shootDelay = 1; //boolean to see if the enemy has walked off of a platform. bool onPlatform = true; void Start() { //locate the player object on the first frame of the scene player = GameObject.FindGameObjectWithTag("Player"); //get the rigidbody of the enemy on the first frame of the scene rb = GetComponent(); } // Update is called once per frame void Update() { //set the rotation value for turning the enemy to the player object //rotation = Quaternion.LookRotation(player.transform.position - transform.position, transform.TransformDirection(Vector3.up)); //Turn the enemy to look towards the player gameObject.transform.LookAt(player.transform.position); //rotate the object, but lock the rotation in the x and y axes transform.rotation = new Quaternion(0, 0, rotation.z, rotation.w); //adding time to the timer for the shoot delay timer += Time.fixedDeltaTime; //if the timer has counted higher than the shoot delay and player is within range, run the if statement if (timer > shootDelay && Vector3.Distance(transform.position, player.transform.position) < enemyRange) { // Shoot the bullet ShootBullet(); //reset the timer timer = 0f; } } void FixedUpdate() { //this if statement enables movement when on a platform, but allows the enemy to drop when not on a platform if(onPlatform) { //Local variable that gets Enemy y position float y = transform.position.y; //set the direction that the enemy will move towards Vector2 moveTo = Vector2.MoveTowards(transform.position, player.transform.position, moveSpeed * Time.fixedDeltaTime); //Sets the enemy movement to move only horizontally moveTo.y = y; //if statement that will only enable the move command if the player is within range //distance method measures the distance from the enemy object to the player //if the distance is less than the enemy range setting, then the enemy will move. if (Vector3.Distance(transform.position, player.transform.position) < enemyRange) { //move the enemy to the player rb.MovePosition(moveTo); } } } void ShootBullet() { //Get the direction from the enemy to the player in the x axis float x = player.transform.position.x - transform.position.x; //Create bullet with correct 3d rotation GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.Euler(0f, 0f, 1.6f)); // Make sure the bullet does not hit the player // You may have to change the two collider types depending // on what you are using Physics2D.IgnoreCollision(bullet.GetComponent(), GetComponent()); //get the rigidbody component of the bullet object Rigidbody2D rb = bullet.GetComponent(); // Add force to the bullet so it flies off if(x >0) { //If Player is to the right of the enemy, shoot right rb.AddForce(new Vector2(1,0)*bulletSpeed); } else { //If player is to the left of the enemy, shoot left rb.AddForce(new Vector2(-1, 0) * bulletSpeed); } } private void OnTriggerExit2D(Collider2D collision) { if(collision.tag == "Ground") { //bool set to false when the enemy walks off of a platform onPlatform = false; } } private void OnTriggerEnter2D(Collider2D collision) { if (collision.tag == "Ground") { //bool set to true when the enemy lands on a platform onPlatform = true; } if(collision.tag == "PlayerBullet") { //Enemy takes damage here, if you would like a different damage you can use the -= statement to remove more. EnemyHealth--; if(EnemyHealth <= 0) { //When the enemy health drops below 1, enemy is destroyed. Destroy(gameObject); } } } }