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 D4rks1027 · May 07, 2012 at 07:15 PM · attackenemiesmelee

Melee attacking multiple enemies

Hi everyone, i use raycasting for my melee combat, however i am unable to target a new enemy once the first one targeted is destroyed. Basically what i do is create a new gameObject variable when the raycast hits an object which then uses SendMessage to the hit object in order to deal damage Here is my script code:

function Start () {

}

function Update () {

 var hit : RaycastHit;
 var fwd = transform.TransformDirection(Vector3.forward);
 
 
 if(Input.GetMouseButtonDown(0)){
     if(Physics.Raycast(transform.position, transform.forward,hit, 3)){
             if(hit.collider.gameObject.tag == "Enemy"){
                 var newEnemy = GameObject.FindWithTag("Enemy");
                 newEnemy.SendMessage("takeDamage", 10);
                 
             }
             else if (hit.collider.gameObject.tag == "testCol"){
                 Debug.Log("item hit");
             }
         }
 }

}

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
0

Answer by GC1983 · May 07, 2012 at 08:47 PM

If you have multiple enemies tagged "Enemy", it will only assign one. What the engine will do is assign the first tagged enemy and only that enemy. Once you kill it you will have nothing else to be able to hit.

Take "var newEnemy = GameObject.FindWithTag("Enemy");" out of that function and place it in Awake(). This will initialize it once the scene starts; thats all you need. And then alter it to create an array: "var newEnemy : Transform[];" and then change your object locator code to var newEnemy = GameObject.FindGameObjectsWithTag("Enemy"). This will locate every enemy in the scene. And then you use a for loop to search for every enemy within your collider function. This will constantly update your enemy list and should let you hit any of them.

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 DanceWreck · Feb 06, 2013 at 09:34 AM 0
Share

so... I'm here bashing my head on the keyboard figuring out something similar in C#...

avatar image GC1983 · Feb 06, 2013 at 09:41 AM 0
Share

Like what?

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

6 People are following this question.

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

Related Questions

please help with my melee attack animation 1 Answer

Implementing a few special attacks for a boss 0 Answers

Melee Attack C# Script Help 1 Answer

Enemy will only take (melee) damage when the distance is below 1 (ie. A zero with decimals after it) 2 Answers

Not identifying the collider? 2 Answers


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