- Home /
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 :)
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!
Answer by SkaredCreations · Dec 17, 2014 at 12:16 PM
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
Perfect! Works like a dream! One other question: If I was to make the enemy shoot to where the player will be, any tips on that? Thanks so much!
You're talking about client prediction, that's a totally different question and not trivial but you'll better search Google for calculations and methods about prediction (may be not directly connected to Unity because this is a generic topic). Also think about how frustrating would be for the user if the enemies are so super smart to intercept them while walking/running, I think player's fun is lot more important than AI in games :)
Your answer
Follow this Question
Related Questions
my enemy is broken 1 Answer
Multiple Cars not working 1 Answer
C#2d Attacking multiple enemies rather than one 2 Answers
2d c# boss follow code 0 Answers
Enemy bounce from screen edges 0 Answers