using System.Collections; using System.Collections.Generic; using UnityEngine; public class EnemyMove : MonoBehaviour { //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; //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; // number of bullets enemy shoots (in a fan like a shotgun) public int numBullets = 1; //Original offset between each bullet public float bulletOffset = 0.1f; 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, run the if statement if(timer > shootDelay ) { // Shoot the bullet ShootBullet(); //reset the timer timer = 0f; } } void FixedUpdate() { //set the direction that the enemy will move towards Vector2 moveTo = Vector2.MoveTowards(transform.position, player.transform.position, moveSpeed * Time.fixedDeltaTime); //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 Vector3 dir = player.transform.position - transform.position; //normalize the values so they do not jump dir.Normalize(); //setting the spread in the x direction for each bullet dir.x -= bulletOffset * numBullets /2; //setting the spread in the y direction for each bullet dir.y -= bulletOffset * numBullets /2; //For loop creates the number of bullets set by designer for(int i = 0; i < numBullets; i++) { //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 rb.AddForce(dir * bulletSpeed); //add to x offset for bullet fan dir.x += bulletOffset; //add to y offset for bullet fan dir.y += bulletOffset; } } }