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
1
Question by EFNova · Apr 15, 2015 at 07:42 PM · collisionreferencemelee

Get a game object's reference from collision?

I'm attempting to make a melee script for my player character, I have placed a trigger sphere collider in-front of the player, and was wondering if I could use this collider to get the reference of the gameobject of any enemy that enters the collider, and then use that reference to get the enemy's health script, and call a function on it.

     void OnTriggerStay(Collider other) 
     {
         if (other.gameObject.tag.Equals("Enemy"))
             enemyGameObject = other.gameObject; // or something like this?       
     }



If this is possible, could someone please tell me the proper way to do it, or give me advice on a better way to do melee? (im a novice so the simpler the better)

thanks in advance :)

Comment
Add comment · Show 1
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 AlwaysSunny · Apr 15, 2015 at 07:28 PM 0
Share

Yeah, there's nothing "wrong" with this. I've certainly heard of folks using this approach for melee detection; getting fancier, you might have an animation of the arms, legs, or weapon props which have triggers attached; this would offer more specific detection (where the hit occurs; what body part or weapon did the hitting).

Checking for a tag is not essential; putting your objects on specific layers that allow collision detection is cheaper where possible; checking for a script is a more direct approach than checking for a tag or name.

 AgentScript agent = other.GetComponent<AgentScript>() as AgentScript;
 if (agent) agent.TakeDamage(someDamage);

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Defetysta · Nov 21, 2018 at 02:21 PM

Hi there,

First of all, if you want to detect enemy getting hit, it's better to use OnCollisionEnter, since OnCollisionStay is called each and every frame, so it would get called probably more times than you intended.

The code you are interested in looks probably something like this

 private void OnCollisionEnter( Collision collision )
     {
         GameObject other = collision.gameObject;
         if (other.CompareTag("Enemy"))
         {
             other.GetComponent<EnemyController>().CallYourFunctionHere();
         }
     }

Second of all, you can cache reference to collision.gameObject so you don't have to reference it twice.

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

Answer by unity_cE1vOYgSOQANyg · Nov 21, 2018 at 12:22 PM

the code showed in the question should if you attach it to the gameObject that collides with the enemy like the main character, then the other argument in the void OnTriggerStay(Collider other) will be a reference to any object the main character collides with, then you can do something like rest health

 void OnTriggerStay(Collider other) 
      {
            if (other.gameObject.tag.Equals("Enemy")) {
                  other. health = other. health - 2;
            }
      }





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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Problem with melee collision 3 Answers

Melee weapon collision inconsistent at low framerates 1 Answer

How to stop my sword from hitting myself 2 Answers

Javascript score problems whilst referencing scripts. 2 Answers

Object reference not set to an instance of an object 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