• 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 connorwforman · Sep 07, 2017 at 10:02 PM · scripting beginner

Enemy Damage Script?

 public int EnemyHealth = 100; 
 public int bulletDamage = 33; 
 public GameObject Bullet; 
 public GameObject enemySelf; 
 
 void Update () {
        if (//"Bullet" is 0 away from "enemySelf") {
                  //subtract "bulletDamage" from "EnemyHealth"; 
                  Destroy(enemySelf); 
 
        }
 }
Comment
Add comment · Show 5
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 MacDx · Sep 07, 2017 at 10:52 PM 1
Share

And the question is...?

avatar image $$anonymous$$ · Sep 07, 2017 at 11:08 PM 0
Share

//"Bullet" is 0 away from "enemySelf"

 Vector2.Distance(Bullet.transform.position, enemySelf.transform.position) == 0

//subtract "bulletDamage" from "EnemyHealth"

 EnemyHealth -= bulletDamage;
avatar image lorenzofman2 $$anonymous$$ · Sep 07, 2017 at 11:49 PM 0
Share

I think that comparing the positions with distance won't work so well (Probably). The bullet will (I guess) travel pretty fast and in no moment it's going to be exactly at the same position as the enemy.

avatar image $$anonymous$$ lorenzofman2 · Sep 08, 2017 at 01:05 AM 0
Share

True, but without knowing more information I can only say that will work if you in fact want to know if the bullet's transform.position is the same as the enemy transform.position, which I agree would be highly unlikely. It would make more sense to have a collider on the enemy and bullet and have the bullet destroy the enemy or the enemy destroy itself using the OnTriggerEnter function

avatar image lorenzofman2 · Sep 07, 2017 at 11:51 PM 0
Share

This script is going to be attached to the player, to the bullet or the enemy? It's important we know to help you.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ollie_payne · Sep 07, 2017 at 11:27 PM

Use either OnCollisionEnter or OnTriggerEnter if you're firing a physical bullet. Alternatively if you don't want to fire a physical bullet use raycasting.

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 connorwforman · Sep 09, 2017 at 02:58 PM 0
Share

It is not actually firing forward at all. It is making a cube with the dimensions .2, .2, 390 being shown waiting .2 seconds then hiding. So, when the bullet appears through the enemy, the health should hopefully reduce. So if you could help me with that, that would be fabulous.

avatar image
0

Answer by PersianKiller · Sep 09, 2017 at 03:41 PM

dude ,do it nice and simple.create an EnemyHealthManager.then create a script for bullets . so when the bullet hit your character it access to your EnemyHealthManager and reduce health of your enemy.

it's enemy healthManager script.attach it to your enemy.

          using System.Collections;
      using System.Collections.Generic;
        using UnityEngine;

         public class EnemyHealthManager : MonoBehaviour {

 public int HP;

             public void DamageToEnemy(int dmg){
            HP-=dmg;
            if(HP<=0){
            //KILL enemy;
                     }

     }
  

}

and it's the bullet script.attach it to your bullet .

                 public class bullet : MonoBehaviour {

 public int DamageToEnemy;
      
                  void OnTriggerEnter2D(Collider2D other){
     if (other.tag == "Enemy") {
         other.GetComponent<EnemyHealthManager> ().DamageToEnemy(DamageToEnemy);
         Destroy (gameObject);

     }
 }


}

and also remember that you should add boxCollider2D component to your enemy and bullet.and your bullet boxcollider2d should be trigger. and your enemy should have RigidBody2D component.and also you should create a tag named Enemy.and make tag of your enemy object to Enemy.

hope it helps.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

74 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

Related Questions

Melee & range ai 0 Answers

How to move an airplane in the Z axis? 0 Answers

Getting an error while trying to get scene name 1 Answer

How to make a collision script 1 Answer

GetAxis to GetTouch 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