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 DubstepDragon · Mar 02, 2013 at 07:38 PM · colliderprojectileattack

Different types of projectiles cause different amounts of damage...

I have an enemy HP script. It functions off the very basic: two int-s, one for current health, and one for maximum health. Now I want to reduce the current health when a specific object with a specific tag collides with it. How can this be achieved? please consider that all my work is in c#.

Thanks in advance!

Comment
Add comment · Show 2
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 DubstepDragon · Mar 02, 2013 at 07:39 PM 0
Share

Please do not post me links from the unity docs. I am not stupid, I can use a search bar, but I need further support, so I asked a question here...

avatar image BlackWingsCorp · Mar 02, 2013 at 08:15 PM 0
Share

use a variable of type RaycastHit and attach it to your gun make sure it's pointed forward and another variable that contains the ray's length (using same velocity as the bullet is advised) (= Vector3.forward * rayLength) but I don't recommend using tags because unless tag every enemy differently if one is hit the others will bi hit aswell. what I do recommend is to use the raycast a bit like this:

 function RayCol (hit : RaycastHit){
 if(Input.GetButtonUp("Fire1"))
 hit.collider.gameObject.currentHealth(-10);
 /*sorry if I made some mistakes I'm used to work with JS but it should give u an idea*/
 

1 Reply

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

Answer by Khada · Mar 03, 2013 at 01:04 AM

If your using the Unity physics engine then we can use the 'OnCollisionEnter' callback on a script attached to the object that will be colliding with the enemy.

 //this is called automatically when two colliders first intersect,
 //(one of the colliding objects must have a rigid body)
 void OnCollisionEnter(Collision collisionInfo)
 {
     if(collisionInfo.gameObject != null && collisionInfo.gameObject.tag == "tagName")
     {
         HealthScript hp = collisionInfo.gameObject.GetComponent<HealthScript>();
 
         if(hp != null)
         {
             hp.ReduceHealth(/*damage here*/);
         }
     }
 }

Or if you're moving things around yourself without rigid bodies etc, we can use raycasting (this is only really suitable for projectiles):

 //replace and update these variables as needed:
 Vector3 direction;
 float speed;
 
 void Update()
 {
     RaycastHit hitInfo;
     Ray ray = new Ray(transform.position, direction.normalized);
 
     if(Physics.Raycast(ray, out hitInfo, speed))
     {
         if(hitInfo.collider != null && hitInfo.collider.tag == "tagName")
         {
             HealthScript hp = hitInfo.collider.GetComponent<HealthScript>();
             if(hp != null)
             {
                 hp.ReduceHealth(/*damage here*/);
             }
         }
     }
 }
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 DubstepDragon · Mar 03, 2013 at 12:40 PM 1
Share

Amazing dude! I am pretty new to raycasting, so I stuck to the other method ins$$anonymous$$d. But I am learning, and this is a good way to learn, so yeah, all my thanks go forward to you!

avatar image Khada · Mar 03, 2013 at 12:42 PM 0
Share

You're welcome.

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

11 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

Related Questions

Damaging enemies... 1 Answer

Scripts not on instantiated GameObjects? 1 Answer

How can i make a projectile attack travel through multiple enemies dealing damage to all of them? 1 Answer

A node in a childnode? 1 Answer

Melee attack comblat 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