Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 TheSaviour · Aug 04, 2015 at 04:13 AM · animationjavascriptevent

How do I destroy a game object when it is hit by a weapon?

So I've been following this tutorial series on youtube. One of the videos in the series teaches you to animate a weapon in Unity and add an event at a particular frame of the animation so that the "enemy" object is destroyed when it is hit by the weapon.

But the problem is that I didn't use Unity to animate my weapon. I used blender. So I don't how to add an event to my animation. Is there an alternative way to do this? And for the record, I'm using javascript for my scripting.

Comment
Add comment · Show 29
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 ownerfate · Aug 04, 2015 at 04:44 AM 0
Share

you would attach a collider to the weapon, if it is a sword or prefab bullet.

this is what i use with my melee weapons, and ranged weapons:

damage.js

 var damage : int = 1000;
 
 function OnCollisionEnter(collision : Collision){
 if(collision.gameObject){
        collision.gameObject.GetComponent(objectHealth).health -= damage;
 }
 }

i don't know if this is what you were asking, but it seemed close.

basically how this would work is you would attach this to the weapon.

then make another script called objectHealth or what have you.

then attach the objectHealth to the thing you want to kill.

the script is really simple just a few lines to interact with the damage script.

objectHealth.js

 var health : int = 1000;
 
 var enemy : GameObject; // make sure you add a collider to the object that holds this script so it will function correctly.
 
 function Update () {
 if(health <= 0){
 
 // Destroy(enemy);
 
 // or something;
 
 }
 }

anything with the objectHealth script on it will be affected, when interacted with the damaging weapon that holds the damage script.

// all you have to do, to make it work when you wanna use onTriggerEnter is

just change :

 function OnCollisionEnter(collision : Collision){
 
 to-
 
 function OnTriggerEnter(collision : Collider){

$$anonymous$$ching my self how to program by using everything from Youtube to the Unity Community, and still learning new things along the way.

avatar image KingLlama · Aug 04, 2015 at 05:59 AM 0
Share

So adding this to my weapon and playing a animation would cause the target to be hit as soon as they make contact through colliders?

avatar image TheSaviour · Aug 04, 2015 at 08:20 AM 0
Share

@ownerfate Unfortunately, the script didn't work. The enemy object still gets destroyed before co$$anonymous$$g in contact with the sword.

avatar image ownerfate · Aug 04, 2015 at 09:07 AM 0
Share

@$$anonymous$$ingLlama : yes, just have to alter the hp of the the enemy, or the damage of the weapon

@theSaviour : by default i made the objectHealth's health 1000 and the damge. damage 1000 so on contact, yes it will destroy the enemy in one hit, set the health to say 2,000 and it will take 2 hits to kill.

you can alter this anyway you see fit.

this is just something to show you, to get an idea if it's something you might be looking for.

avatar image ownerfate · Aug 04, 2015 at 01:05 PM 0
Share

this is a image from my game, with the same script attached to it from the above post i typed, i just removed a lot of other things to make it down to earth.

basically this is what i use the scripts for, $$anonymous$$elee Weapons, and my ranged weapons.

alt text

alt text

006.png (150.6 kB)
004ghostrail-v3.png (28.7 kB)
Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Ibzy · Aug 04, 2015 at 11:22 AM

First thing would be to put a collider on your weapon (I'm imagining sword for this example). Make sure this collider is roughly the right size/shape of your sword, and that it surrounds it nicely.

Secondly, using @ownerfate 's code and applying it to the sword should give the desired effect. The idea is that the object will only be destroyed when the swing of the sword puts it in contact with the object.

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
1

Answer by gunespedastrian · Aug 14, 2015 at 01:55 PM

The easiest way If It's a sword or something, first tag your sword as weapon or something.And add this script to what do you want to destroy.

  function OnCollisionEnter(col : Collision){
  if(col.gameObject.tag == "WeaponOrSomething" ){
        Destroy(gameObject);
        Debug.Log("Wow! ITS WORKS MAN OMG!!");
  }
 }

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
Wiki

Answer by Harardin · Aug 13, 2015 at 03:54 PM

As it’s a gun you can use Raycast from it.

  if (Physics.Raycast(ray, hit) && (hit.transform.gameObject == transform.gameObject))
            {  ...
              Destroy(hit.transform.gameObject); // destroy the object hit
          }
 


Alsow check this for incase http://docs.unity3d.com/ScriptReference/RaycastHit.html

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

7 People are following this question.

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

Related Questions

Can I make animations snap to a frame? 1 Answer

2D character animation walk left and right 2 Answers

How to open a door with the "E" key 1 Answer

How would you reccommend adding the jumping animation to my script? 0 Answers

Material transition through animation 3 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