Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 ReferenceWolf · Sep 30, 2021 at 07:49 PM · attackattacking

How do I fix an issue with my enemy instantly dying?

So I followed a tutorial on youtube, learned what the code meant and altered it slightly to what I needed. It was a tutorial on how to make unity detect clicking and holding. Everything works fine except for the fact that if I release my mouse button when the enemy is colliding with me (I have colliders off for between enemies and players so I guess it's just when the enemy is over the player) the enemy will die. It doesnt matter if hes been previously hit or not. I'd just like to know if anybody may have a solution

Here is my script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerCombat : MonoBehaviour
 {
 
     //all variables
     public Animator animator;
     public Transform attackPoint;
     public float attackRange = 0.5f;
     public LayerMask enemyLayers;
     public int attackDamage = 20;
     public int powerAttackDamage = 40;
     public float nextAttackTime;
     float startTime, endTime;
 
 
     // Update is called once per frame
     void Update()
     {
         // Attack
         
         if(Time.time >= nextAttackTime)
         {
             if (Input.GetButtonDown("Fire1"))
             {
                 Attack();
                 nextAttackTime = Time.time + 1f;
             }
         }
         
                 if(Input.GetButtonDown("Fire1"))
         {
             startTime = Time.time;
         }
 
         if(Input.GetButtonUp("Fire1"))
         {
             endTime = Time.time;
         }
 
         if(endTime - startTime > 0.5f)
         {
             if (Time.time >= nextAttackTime)
             {
                 Attack();
             }
         }
         
     }
 
     void Attack()
     {
         //Play Attack Animation
 
         animator.SetTrigger("Attack");
 
         //Detect Enemies In Range Of Attack
 
         Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
 
         // Damage The Enemy
 
         foreach(Collider2D enemy in hitEnemies)
         {
             enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
         }
     }
 
 
     void OnDrawGizmosSelected()
     {
         if(attackPoint == null)
             return;
 
         Gizmos.DrawWireSphere(attackPoint.position, attackRange);
     }
 }
 

I believe you may only need to look at the "Attack" void and the code that says:

 if(Input.GetButtonDown("Fire1"))
         {
             startTime = Time.time;
         }
 
         if(Input.GetButtonUp("Fire1"))
         {
             endTime = Time.time;
         }
 
         if(endTime - startTime > 0.5f)
         {
             if (Time.time >= nextAttackTime)
             {
                 Attack();
             }
         }

Any help would be greatly appreciated! Thank you!

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
0

Answer by kolalex01 · Oct 01, 2021 at 12:06 PM

Hi, @ReferenceWolf!
Did you check the health of your enemy? It may happen, that you forgot to set his health in the editor - or in the script - or just set it to less than the character's damage and when you damage it once, it dies intantly.

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 ReferenceWolf · Oct 01, 2021 at 06:15 PM 0
Share

Hey there @kolalex01, I made sure my enemy's health is 100, as set in my code. I have no idea what it could be. Thank you for the suggestion though!

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

126 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

Related Questions

Timed Attack System 1 Answer

How can I make an event happen every 2 seconds in the OnTriggerStay void? 2 Answers

Enemy AI problems 2 Answers

C# 2D game enemy multiple grenade attack at once 0 Answers

How can I make my player attack enemies without getting errors? 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