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 /
avatar image
0
Question by Dragonnoble · Dec 06, 2016 at 10:52 PM · playerenemyattacksciptinghealth

I want to attack me enemy and decrease their health, but t won't work, what am I doing wrong with my script?

For some time, I have been looking up tutorials to write a script in which my player can use a weapon and that weapon will damage the enemy and take away their health. Now, I have a script that the enemy damages the layer and takes away their health tat works, so I took it and tried to reverse the parts but it still does not work. What do I need to make it work?

The Script Below:

 var Damage : int = 50;
 var Animation;
 
 private var enemy : GameObject;
 private var enemyHealth : EnemyHealth;
 private var enemyInRange : boolean;
 
 function Awake () 
 {
     Animation = GetComponent.<Animation>();
     
     enemy = GameObject.FindGameObjectWithTag ("Enemy");
     enemyHealth = enemy.GetComponent (EnemyHealth);
 }
 
 function Update () 
 {
     if(Input.GetButtonDown("Fire1"))
     {
         GetComponent.<Animation>().Play("Pick");
     }
 }
 
 function OnTriggerEnter (other : Collider)
     {
       if(other.gameObject == enemy)
         {
           enemyInRange = true;
           Debug.Log("Enemy Hit");
         }
     }
 
 function OnTriggerExit (other : Collider)
     {
       if(other.gameObject == enemy)
         {
           enemyInRange = false;
         }
     }
 
 function Attack ()
    {
      if(enemyHealth.Health > 0)
        {
          enemyHealth.ApplyDamage (Damage);
        }
    }

and the script for my enemy health:

 var Health = 100;
 
 function Update () 
 {
     if(Health <= 0)
     {
         Dead();
     }
 }
 
 function ApplyDamage(TheDamage : int)
     {
         Debug.Log("Damaged");
         Health -= TheDamage;
     }
 
     function Dead() 
     {
         Destroy(gameObject);
     }


Comment
Add comment · Show 1
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 getyour411 · Dec 06, 2016 at 10:53 PM 0
Share

What calls "Attack()"?

4 Replies

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

Answer by rimawi · Dec 07, 2016 at 12:07 AM

You need to call Damage function. your attack which is supposed to call damage is never called.

 function OnTriggerEnter (other : Collider)
      {
        if(other.gameObject == enemy)
          {
            enemyInRange = true;
            Debug.Log("Enemy Hit");
 Attack();
          }
      }


---------THE ATTACK FUNCTION--------------

  function Attack () { 
     Debug.Log("BEING ATTACKED");
      if(enemyHealth.Health > 0) { 
     enemyHealth.ApplyDamage (Damage); 
     } 
     }

Comment
Add comment · Show 5 · 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 rimawi · Dec 07, 2016 at 01:48 PM 0
Share

sorry attack should be Attack (); make sure it is initial cap in the code

avatar image Dragonnoble · Dec 07, 2016 at 04:44 PM 0
Share

I aded that in, but it does not seem to have worked

avatar image rimawi Dragonnoble · Dec 07, 2016 at 07:10 PM 0
Share

make sure Attack(); Is with initial cap 2- Add a debug to the Attack function

function Attack () { Debug.Log("BEING ATTAC$$anonymous$$ED"); if(enemyHealth.Health > 0) { enemyHealth.ApplyDamage (Damage); } }

avatar image Dragonnoble rimawi · Dec 08, 2016 at 02:53 PM 0
Share

This works, but a comment below says that it'll only work for one enemy ins$$anonymous$$d of multiple, is this true?

Show more comments
avatar image
0

Answer by RobAnthem · Dec 08, 2016 at 06:46 AM

Okay I wrote up a quick script, attach the ColliderCombat to the weapon, and the EntityScript to the player and the enemies, and just set the EntityType, I also made it so if you want NPC's seperately, the wont be involved in the combat. Also added an HP handler. I have not tested it though. EDIT: The purpose of the "entityCollider" is incase you have other colliders on your player object, it ensures that only the weapon collider and the entity collider can provide the event. Collider Script is:

 using UnityEngine;
 using System.Collections;
 
 public class ColliderCombat : MonoBehaviour
 {
     #region Data
     public int minDamage;
     public int maxDamage;
     #endregion
     void OnCollisionEnter(Collision collision)
     {
         EntityScript defender = collision.collider.gameObject.GetComponent<EntityScript>();
         if (gameObject.GetComponent<EntityScript>().entityType == EntityScript.EntityType.Enemy && 
             defender.entityType == EntityScript.EntityType.Player)
         {
             if (collision.collider == defender.entityCollider)
             {
                 defender.HitPoints -= Random.Range(minDamage, maxDamage);
             }
         }
         else if (gameObject.GetComponent<EntityScript>().entityType == EntityScript.EntityType.Player &&
             defender.entityType == EntityScript.EntityType.Enemy)
         {
             if (collision.collider == defender.entityCollider)
             {
                 defender.HitPoints -= Random.Range(minDamage, maxDamage);
             }
         }
     }

Entity Script:

 using UnityEngine;
 using System.Collections;
 
 public class EntityScript : MonoBehaviour
 {
     #region Data
     public Collider entityCollider;
     public int maxHitPoints = 100;
     private int hitPoints = 100;
     public int hpRegen = 1;
     public int regenInterval = 60;
     private int regenSwitch = 0;
     public int HitPoints { get { return hitPoints; } set
         {
             hitPoints = value;
             if (hitPoints <= 0) { deathEvent(); }
         } }
     public enum EntityType { Player, Enemy, NPC }
     public EntityType entityType;
     #endregion
     void Update()
     {
         regenSwitch++;
         if (regenSwitch > 60)
         {
             regenSwitch = 0;
             if (HitPoints < maxHitPoints - hpRegen)
             {
                 HitPoints += hpRegen;
             }
             else
             {
                 HitPoints = maxHitPoints;
             }
         }
     }
 
     private void deathEvent()
     {
         //Death stuff here
     }
 }
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 Dragonnoble · Dec 08, 2016 at 02:40 PM 0
Share

I would use raycast, but every time itry to use raycast it ends up causing errors or no errors and it doesnt work

avatar image
0

Answer by federicosalgado · Dec 09, 2016 at 06:18 AM

Maybe this Will help

 Enemyscript enemy;
 
 
 
 Void OnTriggerEnter(Collider other){
 If(other.CompareTag("enemy")){
 
 enemy = Other.GetComponent<the enemy script>.();
 enemy.Damage(int);
 
 }
 
 }
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 RobAnthem · Dec 09, 2016 at 07:17 PM 0
Share

Also collider.other is newly deprecated, collision.collider is the main way to check the "other" collider. Or in your code it would be "other.Collider". Since the collision passes a lot of info inside itself, like the points of impact.

avatar image
0

Answer by Tihan-Nico · Dec 09, 2016 at 10:02 PM

@Draggonnoble

Health C#

using UnityEngine; public class Health : MonoBehaviour { public const int maxHealth = 100; %|-1847962930_2|% public void TakeDamage(int amount) %|493011656_3|% currentHealth -= amount; %|2022189198_5|% %|-731059696_6|% %|-183514112_7|% Debug.Log("Dead!"); } %|916447909_10|% }

Bullet C#

using UnityEngine; using System.Collections; public class Bullet : MonoBehaviour { %|420083905_11|% %|1400404717_12|% %|-2020662319_13|% %|464509255_14|% %|-330945156_15|% if (health != null) %|-222940933_17|% health.TakeDamage(10); %|-1844065826_19|% %|59224891_20|% } }

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

8 People are following this question.

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

Related Questions

I use this script, but the enemy lose health if i don´t target him. 1 Answer

How to make my player not lose health when attacking the enemy? 2 Answers

Damage script is screwed up...? what to do? 1 Answer

Script Wont Take away health from my player when hit by enemy bullet. 0 Answers

Attack,Health and enemy health. 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