Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 tobyh_ · Feb 08 at 03:26 AM · unity 2dattacking

I only want the attack to register once. I'm sorta new to Unity and any help would be greatly appreciated!

When I attack my enemy the attack registers multiple times, starting at 30 and going up in damage by 10 every time (the attack is only supposed to take 10 health from the enemy). Also, when I hold down my attack button (mouse0), the attack registers infinite times. Like the title says I only want the attack to register once upon button press. My project is 2d btw.

Here's my code:

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

public class SwordPlayer : MonoBehaviour { private Animator anim;

 public bool playerInRange;
 public Collider2D Collider2D;
 public bool hasSword;
 public float attackRange = 1f;
 public LayerMask enemyLayers;
 public Transform SwordattackPTLeft;
 public Transform SwordattackPTRight;

 void Start()
 {
     anim = GetComponent<Animator>();
     Collider2D = GetComponent<Collider2D>();
 }

 void Update()
 {
     if (Input.GetKey(KeyCode.E) && playerInRange)
     {
         anim.SetBool("hasSword", true);
         hasSword = true;
     }
     //drop sword
     if(Input.GetKey(KeyCode.Q))
     {
         anim.SetBool("hasSword", false);
     }
 }
 private void FixedUpdate()
 {
     if(hasSword)
     {
         SwordAttackLeft();
         SwordAttackRight();
     }
 }
 private void OnTriggerEnter2D(Collider2D other)
 {
     if(other.CompareTag("weapon"))
     {
        playerInRange = true;
     }
 }
 private void OnTriggerExit2D(Collider2D other)
 {
     if(other.CompareTag("weapon"))
     {
         playerInRange = false;
     }
 }
 private void SwordAttackLeft()
 {
     if (Input.GetButton("Fire1") && hasSword)
     {
         //enemy detection
         Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(SwordattackPTLeft.position, attackRange, enemyLayers);
         //damage enemies
         foreach(Collider2D enemy in hitEnemies)
         {
             enemy.GetComponent<healthSystem>().TakeDamage(10);
         }
     }
 }
 private void SwordAttackRight()
 {
     if (Input.GetButton("Fire1") && hasSword)
     {
         //enemy detection
         Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(SwordattackPTRight.position, attackRange, enemyLayers);
         //damage enemies
         foreach(Collider2D enemy in hitEnemies)
         {
             enemy.GetComponent<healthSystem>().TakeDamage(10);
         }
     }
 }
 void OnDrawGizmosSelected()
 {
     if(SwordattackPTLeft == null)
         return;
     
     if(SwordattackPTRight == null)
         return;

     Gizmos.DrawWireSphere(SwordattackPTLeft.position, attackRange);
     Gizmos.DrawWireSphere(SwordattackPTRight.position, attackRange);
 }

}

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 BuzzyRoboYT · Feb 08 at 07:43 AM

I dont understand some of your code, it seems like your just calling SwordAttackLeft and SwordAttackRight at the same time.

But for your problem try adding a cooldown timer. only attack if the timer is <= 0 if it detects Input.GetButtonUp("Fire1") then you make the timer reset.

Here is the complete code (I cant test it so let me know if it doesnt work):

 using UnityEngine;
 public class SwordPlayer : MonoBehaviour
 {
 private Animator anim;
 public bool playerInRange;
 public Collider2D Collider2D;
 public bool hasSword;
 public float attackRange = 1f;
 public LayerMask enemyLayers;
 public Transform SwordattackPTLeft;
 public Transform SwordattackPTRight;
 private bool attacked = false;
 private float timer = 10f;

 void Start()
 {
     anim = GetComponent<Animator>();
     Collider2D = GetComponent<Collider2D>();
 }
 void Update()
 {
     if (Input.GetKey(KeyCode.E) && playerInRange)
     {
         anim.SetBool("hasSword", true);
         hasSword = true;
     }
     //drop sword
     if (Input.GetKey(KeyCode.Q))
     {
         anim.SetBool("hasSword", false);
     }
 }
 private void FixedUpdate()
 {
     if (hasSword)
     {
         SwordAttackLeft();
         SwordAttackRight();
     }

     //cooldown timer
     timer --;
     if(Input.GetButtonUp("Fire1") && attacked)
     {
         timer = 10;
         attacked = false;
     }
 }
 private void OnTriggerEnter2D(Collider2D other)
 {
     if (other.CompareTag("weapon"))
     {
         playerInRange = true;
     }
 }
 private void OnTriggerExit2D(Collider2D other)
 {
     if (other.CompareTag("weapon"))
     {
         playerInRange = false;
     }
 }
 private void SwordAttackLeft()
 {
     if (Input.GetButton("Fire1") && hasSword && timer <= 0)
     {
         //enemy detection
         Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(SwordattackPTLeft.position, attackRange, enemyLayers);

         //bool for cooldown
         attacked = true;

         //damage enemies
         foreach (Collider2D enemy in hitEnemies)
         {
             enemy.GetComponent<healthSystem>().TakeDamage(10);
         }
     }
 }
 private void SwordAttackRight()
 {
     if (Input.GetButton("Fire1") && hasSword && timer <= 0)
     {
         //enemy detection
         Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(SwordattackPTRight.position, attackRange, enemyLayers);

         //bool for cooldown
         attacked = true;

         //damage enemies
         foreach (Collider2D enemy in hitEnemies)
         {
             enemy.GetComponent<healthSystem>().TakeDamage(10);
         }
     }
 }
 void OnDrawGizmosSelected()
 {
     if (SwordattackPTLeft == null)
         return;

     if (SwordattackPTRight == null)
         return;
     Gizmos.DrawWireSphere(SwordattackPTLeft.position, attackRange);
     Gizmos.DrawWireSphere(SwordattackPTRight.position, attackRange);
 }

} `

Comment
Add comment · Show 4 · 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 tobyh_ · Feb 08 at 11:20 PM 0
Share

Thank you for helping me out. However it doesn't seem to work, the same problem is happening :/ I got a bit confused so I started to randomly try things before asking on here, sorry that's why the code is a bit confusing.

avatar image BuzzyRoboYT tobyh_ · Feb 10 at 03:33 PM 0
Share

I tried to clean up the code a bit and got the attack working.

 using UnityEngine;

 public class SwordPlayer : MonoBehaviour
 {
     private Animator anim;
     public bool playerInRange;
     public Collider2D collider2D;
     public bool hasSword;
     public float attackRange = 1f;
     public LayerMask enemyLayers;
     public Transform SwordAttackPoint;
     private bool attacked = false;
     void Start()
     {
         anim = GetComponent<Animator>();
         collider2D = GetComponent<Collider2D>();
     }
     void Update()
     {
         if (Input.GetKey(KeyCode.E) && playerInRange)
         {
             anim.SetBool("hasSword", true);
             hasSword = true;
             //Destroy weapon object on floor
         }
         //drop sword
         if (Input.GetKey(KeyCode.Q))
         {
             anim.SetBool("hasSword", false);
             //Instantiate new weapon object on floor
         }
     }
     private void FixedUpdate()
     {
         if (Input.GetButton("Fire1") && attacked == false && hasSword)
         {
             SwordAttack();
         }
         if (Input.GetButtonUp("Fire1") && attacked)
         {
             attacked = false;
         }
     }
     private void OnTriggerEnter2D(Collider2D other)
     {
         if (other.CompareTag("weapon"))
         {
             playerInRange = true;
         }
     }
     private void OnTriggerExit2D(Collider2D other)
     {
         if (other.CompareTag("weapon"))
         {
             playerInRange = false;
         }
     }
     private void SwordAttack()
     {
         Debug.Log("Attack!");
         //enemy detection
         Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(SwordAttackPoint.position, attackRange, enemyLayers);
         //damage enemies
         foreach (Collider2D enemy in hitEnemies)
         {
             enemy.gameObject.GetComponent<healthSystem>().TakeDamage(10);
         }
         attacked = true;
     }
     void OnDrawGizmosSelected()
     {
         if (SwordAttackPoint == null)
         {
             return;
         }
         Gizmos.DrawWireSphere(SwordAttackPoint.position, attackRange);
     }
 }
avatar image tobyh_ BuzzyRoboYT · Feb 10 at 10:41 PM 0
Share

Thank you so much! This helped a lot, I've been stuck on this for a while and it made me start to lose interest in the project I'm working on, but this being solved has helped me get back into it. I cannot thank you enough!

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

143 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

Related Questions

Unity Editor not loading/working 2 Answers

How do I get the object from an adjacent space 2d grid 0 Answers

Replicating MC Redstone Wiring 3 Answers

How to make a moveable lever? (2D) 0 Answers

How to give star(reward) only during the first time the mission is completed? 2 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