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 WesterlyCarrot9 · Jan 29, 2014 at 10:06 PM · javascriptmouseenemytargetattack

Attack/Targeting Script Issue

Hello! I have those two scripts below. What they basically allow me to do is to kind of attack an enemy if the mouse cursor is on the object. The issue here is when i have multiple enemies. For some reason i cannot attack whichever enemy i want and i have to start by killing the last one i placed in the scene :S Is there a way to fix this? Thanks!

 MeleeDamage.js
 
 var mDPS : int;
 var oDPS : int;
 
 function mainhand(){
     mDPS = 8;
 }
 
 function offhand(){
     oDPS = 4;
 }
 
 function Update(){
     if(Input.GetKeyDown(KeyCode.R)){
         mainhand();
         Debug.Log("MainHand Equipped!");
     }
     if(Input.GetKeyDown(KeyCode.T)){
         offhand();
         Debug.Log("OffHand Equipped!");
     }
     enemy = GameObject.FindWithTag("enemy");
     var enScript = enemy.GetComponent(Enemy);
     if(enScript.readyforattack){
         if(Input.GetMouseButtonDown(0)){
             damage();
         }    
     }
 }    
     
 
 function damage(){
     enemy = GameObject.FindWithTag("enemy");
     var enScript = enemy.GetComponent(Enemy);
     var statScript = transform.GetComponent(Attributes);
     dmg = mDPS + oDPS + (statScript.STRENGTH/2);
     enScript.health -= dmg;
     statScript.recalculateStats();
     Debug.Log(dmg);
 }


 Enemy.js
 
 var health : int = 20;
 var readyforattack : boolean = false;
 
 function Update(){
     if(health <= 0){
         Destroy(gameObject);
     }
 }
 
 function OnMouseOver(){
     readyforattack = true;
 }
 
 function OnMouseExit(){
     readyforattack = false;
 }    

 
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
1
Best Answer

Answer by Headworker · Jan 29, 2014 at 10:33 PM

Hello. The Problem is this line:

 enemy = GameObject.FindWithTag("enemy");

If you got more than one enemy, it will only return one.

You can do the following to fix that: Add a Collider marked as Trigger to the Player. If you already have a collider, use a child gameobject, if you need help with that, just ask.

Add the following to a script on the player:

     // sorry for csharp i dont use unityscript :)
     // put this on player
     
     // on top
     
     // in start or awake 
 
     List<Enemy> InRange = new List<Enemy>();
     
     // function for the trigger you put on the player object
     
     void OnTriggerEnter(Collider collider)
     {
         if (collider.tag == "Enemy")
         {
             InRange.Add(collider.gameObject.GetComponent<Enemy>());
         }
     }
 
 
     void OnTriggerExit(Collider collider)
     {
         if (collider.tag == "Enemy")
         { 
         InRange.Remove(collider.transform.GetComponent<Enemy>())
         }   
     }
 
     // Now in your attack function you can do the damage loop:
 
     void YourAttackFunction()
     {
         for (int x = 0; x < InRange.Count; x++)
         {
             InRange[x].ApplyDamage();
         }
     
     }
     
 }

Note 1: There are many more possible way to do this, using raycasting to the middle of the screen for instance. If you want to know more.... :)

Note 2: Sorry I will need to find out how to format things properly here, tab is not working for me

Comment
Add comment · Show 2 · 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 WesterlyCarrot9 · Jan 29, 2014 at 11:32 PM 0
Share

I see. How could this be done with Raycasting?

avatar image Headworker · Jan 30, 2014 at 11:01 AM 0
Share
 void Attack()
 {
     Ray TheRay = Camera.main.ScreenPointToRay(new Vector3(0.5f, 0.5f, 0));
     RaycastHit rayhit;
     if (Physics.Raycast(TheRay,out rayhit,5f)) // Here you can put the right distance for you purposes
     {
         if (rayhit.collider.tag == "Enemy")
         { 
             rayhit.collider.GetComponent<Enemy>().damage$$anonymous$$e() // Fill in your function within the 'enemy' script            
         }
     
     
     }
 
 
 
 }

This shoots an ray out from the camera through the middle of the screen. The first enemy touched by it will be applied the damage.

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

19 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 avatar image avatar image

Related Questions

why won't my code work? 1 Answer

Melee range AI 1 Answer

Enemy attack target 1 Answer

I use this script, but the enemy lose health if i don´t target him. 1 Answer

Need Help With My AI Script 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