Making an enemy shoot at the Players direction (not just up, down, left and right)

Ok, so I’m going pretty well on my game at the moment, but have just come into a slight hiccup but the enemy only being able to shoot in the directions of up, down, left and right, but I would rather the enemy always be able to shoot at the player.

***Ignore the parts of the code that dont really do anything, thats just my testing :slight_smile:

My code:

using UnityEngine;
using System.Collections;

public class AttackingMob : Entity {

	public GameObject attackingg;
	
	private bool lookFoward;
	private bool lookBackward;
	private bool lookLeft;
	private bool lookRight;

	public Entity attacking;
	public int distance;
	public int minDist = 2;
	//private int maxDist = 5;
	
	private bool canAttack;	
	
	public Rigidbody2D enemyBullet;
	public float enemyAttackRate = 0.25f;
	public float enemyCooldown;

	void Start () {
		canAttack = true;
		if(attacking == null){
			attackingg = GameObject.FindGameObjectWithTag ("Player");
			attacking = attackingg.GetComponent<Entity>();
		}
	}
	
	void Update () {
		
		if(Vector3.Distance(transform.position,attackingg.transform.position) <= minDist){
			if(attacking.rigidbody2D.transform.position.y > (rigidbody2D.transform.position.y + distance)) {
				rigidbody2D.transform.position += Vector3.up * speed * Time.deltaTime;
				lookFoward = true;
				lookBackward = false;
				lookLeft = false;
				lookRight = false;
			}
			if(attacking.rigidbody2D.transform.position.y < (rigidbody2D.transform.position.y - distance)) {
				rigidbody2D.transform.position += Vector3.down * speed * Time.deltaTime;
				lookFoward = false;
				lookBackward = true;
				lookLeft = false;
				lookRight = false;
			}
			if(attacking.rigidbody2D.transform.position.x > (rigidbody2D.transform.position.x + distance)) {
				rigidbody2D.transform.position += Vector3.right * speed * Time.deltaTime;
				lookFoward = false;
				lookBackward = false;
				lookLeft = false;
				lookRight = true;
			}
			if(attacking.rigidbody2D.transform.position.x < (rigidbody2D.transform.position.x - distance)) {
				rigidbody2D.transform.position += Vector3.left * speed * Time.deltaTime;
				lookFoward = false;
				lookBackward = false;
				lookLeft = true;
				lookRight = false;
			}
		}
		
		if(Vector3.Distance(transform.position,attackingg.transform.position) <= minDist && canAttack){
			if (Time.time >= enemyCooldown){
				shootAtPlayer();
				StartCoroutine (waitForAttack ());
			}
		}
	
	//	if(Vector2.Distance (rigidbody2D.transform.position, attacking.transform.position) <= distance && canAttack){
		//	attackEntity();
		//	StartCoroutine (waitForAttack ());
		//}
		if(health <=0){
			Die ();
		}
	}
	
	public void Die(){
		Destroy (gameObject);
	}
	
	public void EnemyDamaged(int amount){
		amount = Staff.realDamage;
		if (health > 0) {
			health -= amount;
		}
		
		if (health <= 0) {
			health = 0	;	
			Destroy(gameObject);
		}
	}
	
	public void shootAtPlayer(){
		if (lookRight) {
			Rigidbody2D bPrefab = Instantiate (enemyBullet, transform.position, Quaternion.identity) as Rigidbody2D;
			bPrefab.rigidbody2D.AddForce (Vector2.right * 650);
			enemyCooldown = Time.time + enemyAttackRate;
		} 
		if(lookLeft) {
			Rigidbody2D bPrefab = Instantiate (enemyBullet, transform.position, Quaternion.identity) as Rigidbody2D;
			bPrefab.rigidbody2D.AddForce (-Vector2.right * 650);
			enemyCooldown = Time.time + enemyAttackRate;
		}
		if(lookFoward) {
			Rigidbody2D bPrefab = Instantiate (enemyBullet, transform.position, Quaternion.identity) as Rigidbody2D;
			bPrefab.rigidbody2D.AddForce (Vector2.up * 650);
			enemyCooldown = Time.time + enemyAttackRate;
		}
		if(lookBackward) {
			Rigidbody2D bPrefab = Instantiate (enemyBullet, transform.position, Quaternion.identity) as Rigidbody2D;
			bPrefab.rigidbody2D.AddForce (-Vector2.up * 650);
			enemyCooldown = Time.time + enemyAttackRate;
		}
	}
	
	public void attackEntity(){
		int take = Random.Range (1, 20);
		attacking.takeHealth (take);
		//playerBlood.Play();
	}
	
	IEnumerator waitForAttack(){
		canAttack = false;
		yield return new WaitForSeconds(2);
		canAttack = true;
	}
}

Any help is appreciated!

I won’t read such long code, so I just point out in the right direction: you will get the direction from the enemy to the player by using (playerTransform.position - enemyTransform.position).normalized