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 esitoinatteso · Oct 11, 2013 at 03:09 PM · collisioncolliderdamagehow-to

How to apply damage without Raycast

Hi there! Sorry if this question may look inappropriate but I can't find a solution on my own. First let me try to explain you what I'd like to do: I'd like my enemy to decrease its life of an amount called damage, specified by the bullet with which it collides.

I'm using instantiate and prefabs to handle the shootings, not Raycast, so I can't benefit from RaycastHit. All my bullets are prefab that player can instantiate.

So right now I'm checking for collisions with OnTriggerEnter, and then I'm applying the damage. If I had just one kind of bullet that would be done... but I'm planning to add more bullets to my game, so it'd be painful to write a specific OnTriggerEnter for every one of them, considering that I'd also have to access the damage of every bullet.

How can I do to make my enemy recognize which bullet hit it, so that it automatically decrease the right amount of health on every hit? I can't help myself thinking that this must be something obvious...

Here is a part of my enemy script:

     void OnTriggerEnter(Collider collider){
         
         if(collider.gameObject.CompareTag("Shot")){
             
             AdjustHealth(??????); //I'd like to put in those brackets the current bullet's damage
             
         }
         
     }
     
     void AdjustHealth(float adj){
         
         currentHealth += adj;
         if(currentHealth <= 0)
             Destroy(this.gameObject);
     }
 }

Code is appreciated, but I'd prefer you to point me in the right direction instead of just feeding me with the solution... thanks for your time!

Comment
Add comment · Show 4
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 ShadoX · Oct 11, 2013 at 03:34 PM 1
Share

You could just check if the collider contains the damage variable and use that assu$$anonymous$$g that the variable is always named the same. Something like

  void OnTriggerEnter(Collider collider){
        if(collider.gameObject.CompareTag("Shot")){
            bulletScript script = collider.gameObject.GetComponent<BullectScript>();
          if(script!=null){
                AdjustHealth(script.damage);
          }
        }
  
     }

should work just fine assu$$anonymous$$g that the damage variable is public. If not - call a method that returns the value.

avatar image esitoinatteso · Oct 11, 2013 at 04:30 PM 0
Share

Hi ShadoX and thanks for your comment! Let's see if I've understood correctly: that way I won't need to reiterate anything when the enemy collides with another bullet? I just have to give all my bullets the same variable " damage"?

avatar image $$anonymous$$ · Oct 11, 2013 at 05:08 PM 0
Share

Upvoted for expressing eagerness to learn over getting a complete solution.

avatar image ShadoX · Oct 11, 2013 at 08:57 PM 0
Share

@esitoinatteso Yup, at last as long as you use the same script on all bullets and just modify the damage variable.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by whydoidoit · Oct 11, 2013 at 04:48 PM

Just send a message to the other object from the bullet, if it has an AdjustHealth function it will be called:

   public float damage = 10;

   void OnTriggerEnter(Collider otherCollider)
   {
        otherCollider.SendMessage("AdjustHealth", damage, SendMessageOptions.DontRequireReceiver);
   }
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 esitoinatteso · Oct 11, 2013 at 05:12 PM 0
Share

That seems just what I was looking for! Unfortunately I've messed up with the project I was working and I can't test it right now, but as soon as I can, I'll try that and update your kind answer as THE answer if it turns out to work as intended... meanwhile thanks!

avatar image esitoinatteso · Oct 13, 2013 at 02:24 PM 0
Share

Awesome, thanks a lot!

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

17 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

Related Questions

Copy a damage value on collision 2 Answers

Damage on collision with player.. 1 Answer

Collision, tags, and triggers 1 Answer

This collision Enemy health code suddenly stopped working. 1 Answer

Damage over time while colliding. 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