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 Madkrumper9 · Nov 10, 2020 at 02:34 PM · projectiledamagechildrensendmessagefunction call

Child object collision

I have a ship with multiple pieces of wood like a real ship. each piece has a collider and the same script:

     public void ApplyDamage(int damageTotal)
     {
         health -= damageTotal;
         Debug.Log("Damage " + damageTotal + " was applied");
         Destroy(gameObject);
     }

My goal is to check which piece got hit with a cannonball using send message or any other way. how do i get it to send message to the right piece.

Cannonball script:

 void OnCollisionEnter(Collision hit)
       {
             Debug.Log("Collision Detected");
             if (hit.gameObject.tag == "ShipPlate")
             { 
                 hit.gameObject.SendMessage("ApplyDamage", 5); // send the message to its owner
                 Debug.Log("aaa");
                 Destroy(gameObject);
             }
         }



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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by razzraziel · Nov 10, 2020 at 05:53 PM

You should raycast and then use its hit property to determine which object gets hit by raycast. Don't use physics collisions, they probably cost more and most of the times are not accurate because of fixed update.

Also if it doesnt work correct, you possibly put script on wrong object (not pieces, on cannon).

Comment
Add comment · Show 4 · 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 Madkrumper9 · Nov 11, 2020 at 12:15 AM 0
Share

The cannons are physics based projectiles though. I'm new to program$$anonymous$$g so how would you go about doing that?

avatar image razzraziel Madkrumper9 · Nov 11, 2020 at 09:11 AM 0
Share

Search for unity bullet hit

avatar image MurphyMurph_21 Madkrumper9 · Nov 11, 2020 at 01:26 PM 0
Share
             **Instead of searching for the tag just use the name.**

       if (hit.gameObject.name == "ShipPlate 1")
                 {
       hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
                     Debug.Log("aaa");
                     Destroy(gameObject);
                 }
             if (hit.gameObject.name == "ShipPlate 2")
             {
       hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
                 Debug.Log("aaa");
                 Destroy(gameObject);
             }
             **or you can do switch/case**

             switch (hit.gameObject.name)
                     {
                         case "Ship 1":
                             hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
                             Debug.Log("aaa");
                             Destroy(gameObject);
                             break;

                         case "Ship 2":
                             hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
                             Debug.Log("aaa");
                             Destroy(gameObject);
                             break;

                         default:
                             Debug.Log("Error/ Couldnt find ship parts");
                             break;

                     }

         }

avatar image Madkrumper9 MurphyMurph_21 · Nov 13, 2020 at 12:03 PM 0
Share

I tried what you said and it didn't ,work this is my new script:

     void OnCollisionEnter(Collision hit)
     {
         Debug.Log("aaa");
         if (hit.gameObject.name == "3,3(Right)")
         {
             hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
             Debug.Log("aaa");
             Destroy(gameObject);
         }
         else if (hit.gameObject.name == "4,3(Right)")
         {
             hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
             Debug.Log("aaa");
             Destroy(gameObject);
         }
         else if (hit.gameObject.name == "4,2(Right)")
         {
             hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
             Debug.Log("aaa");
             Destroy(gameObject);
         }
         else if (hit.gameObject.name == "3,1(Right)")
         {
             hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
             Debug.Log("aaa");
             Destroy(gameObject);
         }
         else if (hit.gameObject.name == "3,2(Right)")
         {
             hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
             Debug.Log("aaa");
             Destroy(gameObject);
         }
         else if (hit.gameObject.name == "3,2(Right)")
         {
             hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
             Debug.Log("aaa");
             Destroy(gameObject);
         }
         else if (hit.gameObject.name == "Ship")
         {
             hit.gameObject.Send$$anonymous$$essage("ApplyDamage", 5); // send the message to its owner
             Debug.Log("hit ship as whole not child");
             Destroy(gameObject);
         }

sadly it only returns the last one. How do i get it to collide with the child objects?

avatar image
0

Answer by MurphyMurph_21 · Nov 13, 2020 at 03:24 PM

@Madkrumper9 I wouldnt use else if I would just use if statements for all of them. Do you have colliders on each child gameobject or just the main ship?

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 Madkrumper9 · Nov 18, 2020 at 03:13 PM 0
Share

each child game object. though this still isn't working it registers a collision but doesn't do anything past that

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

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

Related Questions

Damaging Enemy strangely not working. 0 Answers

how to take health/hearth from player when colliding with an object? 2 Answers

Overloaded functions fail to call 1 Answer

Call function from variable scripts 2 Answers

How do I apply damage to the enemy ai with projectile 0 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