shooting multiple enemies using raycast

im having problems with shooting multiple enemies, i had no problem with one but as soon as i’ve duplicated the enemy ive opened up a whole world of problems it seems…

i have attached the script in the hope that i may have made a small error:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ShootAndTarget : MonoBehaviour {

public List<GameObject> targets;
public float attackTimer;
public float coolDown;

public float range = 1000;
public float force = 1000;
public RaycastHit hit;
public AudioClip gunShot;
public GameObject spark;
public GameObject smoke;
public GameObject Explosion;

public GameObject bulletHole;

// Use this for initialization
void Start () {
   attackTimer = 0;
   coolDown =0.0f;

   GameObject[] enemyTargets = GameObject.FindGameObjectsWithTag("Enemy");
   if (enemyTargets != null)
   {
     foreach(GameObject go in enemyTargets)
     {
      targets.Add(go);
     }
   }

}

// Update is called once per frame
void Update () {

    if(attackTimer > 0)
        attackTimer -= Time.deltaTime;

    if(attackTimer < 0)
        attackTimer = 0;

    if(Input.GetButtonDown("Fire1")){
           if(attackTimer == 0) {
			audio.PlayOneShot(gunShot);
			Rayshoot();
            Attack();
           attackTimer = coolDown;
        }
    }
	
		

}

 public  void Rayshoot(){	
	
   Vector3 direction_ray = transform.TransformDirection(Vector3.forward);

   Debug.DrawRay(transform.position, direction_ray * range, Color.blue);

   if(Physics.Raycast(transform.position, transform.forward, out hit, Mathf.Infinity)){
				
		//Instantiate(bulletHole, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal)); 
		Instantiate(spark, hit.point, Quaternion.identity);
		
	if(hit.collider.tag == "Level"){
		//Instantiate(smoke, hit.point, Quaternion.identity);	
			
		}
		
	if(hit.collider.tag == "Crate"){
		hit.rigidbody.AddForceAtPosition (transform.forward * force, hit.point);	
		hit.rigidbody.AddExplosionForce(force, hit.point, 1);
		}			
		
	if(hit.collider.tag == "Drum"){
		//Instantiate(spark, hit.point, Quaternion.identity);
		hit.rigidbody.AddForceAtPosition (transform.forward * force, hit.point);	
		hit.rigidbody.AddExplosionForce(force, hit.point, 1);					
			}	
		
		
		//Enemy
		
		if(hit.collider.tag == "Enemy"){
			if(hit.transform.name == "Enemy") {
		 	hit.gameobject.getcomponent<EnemyHealth>().AdjustCurrentHealth(-10);
				}
		}
		//	hit.collider.tag = "Dead";
		Instantiate(spark, hit.point, Quaternion.identity);
			
		}
	}

}

Seems like you are trying to just take away health when the raycast hits that target… Try ‘SendMessage’ works great for me!

Your problem when it comes to every Enemy loosing health is your Attack() Function, its looping through all Enemies and attacking them.

Solution:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
 
public class ShootAndTarget : MonoBehaviour {
 
public List<GameObject> targets;
public float attackTimer;
public float coolDown;
 
public float range = 1000;
public float force = 1000;
public RaycastHit hit;
public AudioClip gunShot;
public GameObject spark;
public GameObject smoke;
public GameObject Explosion;
 
public GameObject bulletHole;
 
// Use this for initialization
void Start () {
   attackTimer = 0;
   coolDown =0.0f;
 
   GameObject[] enemyTargets = GameObject.FindGameObjectsWithTag("Enemy");
   if (enemyTargets != null)
   {
     foreach(GameObject go in enemyTargets)
     {
      targets.Add(go);
     }
   }
 
}
 
// Update is called once per frame
void Update () {
 
    if(attackTimer > 0)
        attackTimer -= Time.deltaTime;
 
    if(attackTimer < 0)
        attackTimer = 0;
 
    if(Input.GetButtonDown("Fire1")){
           if(attackTimer == 0) {
         audio.PlayOneShot(gunShot);
         Rayshoot();
            //Attack();  -- NO NEED FOR THIS
           attackTimer = coolDown;
        }
    }
 
 
 
}
 
 public  void Rayshoot(){   
 
   Vector3 direction_ray = transform.TransformDirection(Vector3.forward);
   Debug.DrawRay(transform.position, direction_ray * range, Color.blue);
   if(Physics.Raycast(transform.position, transform.forward, out hit, Mathf.Infinity)){
 
       //Instantiate(bulletHole, hit.point + (hit.normal * floatInFrontOfWall), Quaternion.LookRotation(hit.normal)); 
       Instantiate(spark, hit.point, Quaternion.identity);
 
    if(hit.collider.tag == "Level"){
       //Instantiate(smoke, hit.point, Quaternion.identity); 
 
       }
 
    if(hit.collider.tag == "Crate"){
       hit.rigidbody.AddForceAtPosition (transform.forward * force, hit.point);  
       hit.rigidbody.AddExplosionForce(force, hit.point, 1);
       }        
 
    if(hit.collider.tag == "Drum"){
       //Instantiate(spark, hit.point, Quaternion.identity);
       hit.rigidbody.AddForceAtPosition (transform.forward * force, hit.point);  
       hit.rigidbody.AddExplosionForce(force, hit.point, 1);           
         }    
 
 
       //Enemy
 
       if(hit.collider.tag == "Enemy"){
            hit.gameobject.SendMessage("AdjustCurrentHeath", damage);
       }
       //    hit.collider.tag = "Dead";
      
       Instantiate(spark, hit.point, Quaternion.identity);
 
       }
    }
 /*
private void Attack() {
 
    if (targets == null) return;
 
    foreach (GameObject target in targets)
   {
     if (target != null)
     {
       EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
       eh.AdjustCurrentHeath(-10);
       }
    }
}*/

ok might have sorted it… i dunno if this is 100% sensible but thanks to you markedagain it works i edited yours and it works :smiley:

Thankyou!!!

here is the code:

if(hit.collider.tag == “Enemy”){
EnemyHealth eh = (EnemyHealth)hit.collider.GetComponent(“EnemyHealth”);
eh.AdjustCurrentHeath(-10);

		}