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 BlackWingsCorp · Feb 28, 2013 at 12:32 PM · javascriptraycasthitdamagetargetting

Target a single enemy

Hey guys what's up I'm making an FPS prototype for my game using raycasthit for the gun, but when I hit 1 enemy they all take damage. How do I maket it so that only the shooted enemy takes damage? Here's the script:

 var force : float = 10;
 var range : float = 200;
 var clipnumb : int = 3;
 var bulletsPerClip : int = 14;
 var bulletsLeft : int;
 var reloadTime : float = 1;
 var shootAudio : AudioClip;
 var reloadAudio : AudioClip;
 var enemies : GameObject[];
 var enemy : GameObject;
 
 
 function Start () {
 bulletsLeft = bulletsPerClip;
 enemies = GameObject.FindGameObjectsWithTag("Enemy");
 }
 
 function Update () {
 Shooting();
 Reloading();
 RayShoot();
 }
 
 function RayShoot (){
 var hit : RaycastHit;
 var directionRay = transform.TransformDirection(Vector3.forward);
 Debug.DrawRay(transform.position, directionRay * range, Color.red);
 if(Physics.Raycast(transform.position, directionRay, hit, range)){
 for(var i : int = 0; i < enemies.length; i++){
 var dir : Vector3 = (enemies[i].transform.position - transform.position).normalized;
 var direction : float = Vector3.Dot(dir, transform.forward);
 if(hit.collider.tag == "Enemy"){
 if(direction > 0){
 if(Input.GetButtonUp("Fire1") && bulletsLeft > 0){
 enemy = enemies[i];
 var eh : EnemyHealth = enemy.GetComponent("EnemyHealth");
 eh.AdjustHealth(-10);
 }
 else{
 }
 }
 }
 }
 }
 } 
 
 function Shooting (){
 if(Input.GetButtonUp("Fire1") && bulletsLeft){
 bulletsLeft --;
 if(bulletsLeft < 0){
 bulletsLeft = 0;}
 audio.PlayOneShot(shootAudio);
 }
 }
 
 function Reloading (){
 yield new WaitForSeconds(reloadTime);
 if(Input.GetKeyUp(KeyCode.F)){
 if(clipnumb > 0){
 bulletsLeft = bulletsPerClip;
 audio.PlayOneShot(reloadAudio);
 }
 clipnumb --;
 }
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Sisso · Feb 28, 2013 at 01:17 PM

Your logic here is a little mess. But the main problem is that you iterate over all enemies after got a raycast.

After rayscast, remove your enemies loop and do somehting like it:

 hit.collider.gameObject.GetComponent(EnemyHealth).AdjustHealth(-10);
Comment
Add comment · Show 3 · 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 BlackWingsCorp · Feb 28, 2013 at 02:10 PM 0
Share

Sir(or mam) you are amazing. Thanks :P

avatar image Sisso · Feb 28, 2013 at 02:23 PM 0
Share

You're welcome. But what I did was simple read your logic. :P It is a really good exercise explain your code for for someone, an imaginary one, probably you will got the problem when you said "when raycast hit, for each enemy I will apply damage".

avatar image BlackWingsCorp · Feb 28, 2013 at 03:24 PM 0
Share

A gd exercise but one prob. I'm not a master of JS still have alot to learn.

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

10 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

Related Questions

How to Detect Collision, Calculate Velocity+Mass, Apply Damage 0 Answers

How to damage gameobject via raycast 1 Answer

I'm trying to get a Raycast Laser weapon to tell whatever it hits to do damage, and its not working. im kindof a noob... 0 Answers

Raycast script help? 1 Answer

Raycast Distance Help 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