Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheBrian 666 · Jul 16, 2013 at 08:07 PM · c#raycastfpsshootingenemydamage

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);
             
         }
     }

}

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheBrian 666 · Jul 16, 2013 at 07:59 PM 0
Share

p.s. i know there are similar questions already out there but.... this is a shot attack not a melee attack

avatar image fafase · Jul 16, 2013 at 08:07 PM 0
Share

But what is the issue?

avatar image TheBrian 666 · Jul 16, 2013 at 08:12 PM 0
Share

i cant get it to take away health from the enemy that i hit

avatar image TheBrian 666 · Jul 16, 2013 at 08:16 PM 0
Share

ok i have managed to take away health but unfortunatly now it takes health from every enemy in the scene!! O_O

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ShootAndTarget : $$anonymous$$onoBehaviour {
 
 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, $$anonymous$$athf.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.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);
         }
     }
 }
 

}

avatar image TheBrian 666 · Jul 16, 2013 at 08:54 PM 0
Share

is there anyone that can help??

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by TheBrian 666 · Jul 16, 2013 at 09:19 PM

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 :D

Thankyou!!!!

here is the code:

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

         }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image markedagain · Jul 17, 2013 at 02:44 AM 0
Share

glad u figured it out sir :) gl with the rest

avatar image
0

Answer by Mill0698 · Jul 16, 2013 at 10:15 PM

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);
        }
     }
 }*/


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FPS shooting 1 Answer

How do I make bullet tracers show when I shoot in the air? 1 Answer

Ray.direction editing not working in C#? 1 Answer

Where can i find an easy raycast shooting tutorial for C#? 2 Answers

Raycast shooting for multiple weapons 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges