Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Jun1643 · Mar 21, 2018 at 01:42 AM · collisionraycastattackmonsterhealth

How can i kill my enemy?,How to kill enemy in unity 3d.

In my game, i can shoot bullet but i cannot kill my enemy. I want enemy disappear after i shoot him. Please help me. **I'm using two codes. One is Throwobject and another is monsterattack.

Throwobject

 using UnityEngine;
     using System.Collections;
     
     public class ThrowObject : MonoBehaviour {
     
         public GameObject projectile;
         public AudioClip shootSound;
     
         public GameObject gun;
         public float throwSpeed = 1500f;
         private AudioSource source;
         private float volLowRange = .5f;
         private float volHighRange = 1.0f;
     
     
         void Awake () {
     
             source = GetComponent<AudioSource>();
     
         }
     
     
         void Update () {
     
             if (Input.GetButtonDown("Fire1"))
             {
                 float vol = Random.Range (volLowRange, volHighRange);
                 source.PlayOneShot(shootSound,vol);
                 GameObject throwThis = Instantiate (projectile, transform.position, transform.rotation) as GameObject;
                 gun.transform.DetachChildren ();
                 throwThis.GetComponent<Rigidbody>().AddRelativeForce (new Vector3(0,0,throwSpeed));
     
             }
     
             RaycastHit hit;
             Ray shooterRay = new Ray(transform.position, transform.forward);
             if(Input.GetButton("Fire2")){
                 Debug.DrawRay (transform.position, transform.forward, Color.green);
                 if(Physics.Raycast(shooterRay, out hit, Mathf.Infinity)){
                     if(hit.collider.tag == "Enemy"){
                         Debug.Log("Hit");
                         hit.transform.GetComponent<MonsterAttack>().health -= 10;
                         //hit.collider.gameObject.GetComponent<Animation>().Play("HumanoidJumpUp");
     
                     }
                 }
             }
         }
     }
 
 

and here is monsterattack

 using UnityEngine;
 using System.Collections;
 
 public class MonsterAttack : MonoBehaviour {
     private GlobalInformation gi3;
     public int health;
     public int hit;
     public Transform target;
     //public int maxDistance;
     public int targetDistance;
     private float enemy2char;
     private Transform myTransform;
 
     // Use this for initialization
     void Start () {
         gi3 = GlobalInformation.gi;
         health = 100;
         myTransform = transform;
         GameObject go = GameObject.FindGameObjectWithTag("Player");
 
         target = go.transform;
 
     }
     
     // Update is called once per frame
     void Update () {
 
         if (health <= 0) {
             gi3.addscore (20);
             Destroy (gameObject);
         }
 
         Debug.DrawLine(target.position, myTransform.position, Color.yellow);
 
         enemy2char = Vector3.Distance (target.position, myTransform.position);
 
         /*if((enemy2char < maxDistance) && (enemy2char > targetDistance)) {
             //Move towards target
             Mover();
         }*/
         if (enemy2char <= targetDistance) {
             gi3.adjusthealth (-1);
         }
     }
     void OnTriggerEnter(Collider other)
     {
 
         if (other.gameObject.tag == "Player")
         {
 
             //other.GetComponent("PlayerHealth").AddjustCurrentHealth ();
             gi3.adjusthealth(-10);
         }
         if (other.gameObject.tag == "Enemy")
         {
 
             //other.GetComponent("PlayerHealth").AddjustCurrentHealth ();
             gi3.adjusthealth(-10);
         }
     }
 }
 




I want to make bullet decrease health -20 when it hits monster. Thank you again. ,

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

1 Reply

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

Answer by exzizt · Mar 21, 2018 at 02:15 AM

You can acheive this effect mutliple ways. One way is to do this:

Have a bool for your enemy object called "isDead". When you reduce from your enemy health, check to see if health is

Also, to make your enemy "disappear", if their health is

Comment
Add comment · Show 10 · 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 Jun1643 · Mar 21, 2018 at 03:14 AM 0
Share

Thank you for answer me. Can you tell me how code should be? Thank you

avatar image Jun1643 · Mar 21, 2018 at 03:16 AM 0
Share

And... If i do that way, can i put code in throwobject code?

avatar image exzizt Jun1643 · Mar 21, 2018 at 03:22 AM 1
Share

Ins$$anonymous$$d of doing this:

hit.transform.GetComponent().health -= 10;

You could do:

hit.transform.GetComponent().AddHealth(-10);

And then make the method AddHealth in monster attack, like so:

 public void AddHealth(int amount)
 {
     health += amount;
     
     if (health <= 0) 
     {
         // Do the stuff here you want to do when your enemy is "dead".
         isDead = true;
         // $$anonymous$$ake the enemy "disappear" by destroying the enemy gameobject.
         Destroy(gameObject);
     }
 }
avatar image Jun1643 exzizt · Mar 21, 2018 at 10:14 AM 0
Share

Hi I just put

public bool isDead; and your stuff above but the enemy still doesn't disappear. What should i do??' Thank you

Show more comments

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

199 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 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

RayCast not working with MeshCollider - Unity's roller ball collision works 1 Answer

Raycast ignores objects with colliders 1 Answer

Client can damage host but not vice versa? 0 Answers

OnCollisionEnter2D not working, Again! 0 Answers

How do i deal damage with raycasts in UNET? 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