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 UnityinTraining · Jun 06, 2019 at 05:10 AM · triggerplayer2d-platformerenemyattacking

Why is my character taking damage when attacking & why is it attacking multiple times per hit?

I use a trigger jump attack on my player. The problem is that when I touch the enemy once it takes 40 damage. My player also takes damage whenever I touch the enemy. MY player attack damge is set to 10

Problem Video

PlayerHealth

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 public class PlayerHealth : MonoBehaviour
 {
     public int startingHealth = 200, currentHealth;
     int amount;
     public Slider healthSlider;
 
     Animator anim;
     Player Player; //reference to Player Script
     bool dead;
     bool damage;
 
     // Start is called before the first frame update
     void Awake()
     {
         anim = GetComponent<Animator>();
         Player = GetComponent<Player>();
         currentHealth = startingHealth;
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     public void TakenDmg(int amount)
     {
         damage = true;
 
         currentHealth -= amount;
         healthSlider.value = currentHealth;
 
         //anim.SetBool("Hurting", true);
 
         //if (!damage) TODO: Fix Hurting Animation off
         //{
         //    anim.SetBool("Hurting", false);
         //}
 
         if (currentHealth <= 0 ) {
             Death();
         }
 
     }
 
     public void Death()
     {
         dead = true;
     
         Destroy(gameObject, .5f); //TODO: add in the hurt animation
         Player.enabled = false; //stops player from moving when dead
         SceneManager.LoadScene("MainMenu"); //TODO: Create a wait a few seconds before returning to mainmenu
     }
 
 } //playerHealth



PlayerAttack

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerAttack : MonoBehaviour
 {
     public int attackdmg;
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
         if (other.tag == "Enemy")
         {
             other.GetComponent<OpossumHealth>().damageTaken(attackdmg);
         }
     }
 
 
 
 }
 

OppossumHealth

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class OpossumHealth : MonoBehaviour
 {
     public int enemyHealth = 100, currentHealth;
 
     // Start is called before the first frame update
     void Start()
     {
         currentHealth = enemyHealth;
     }
 
     // Update is called once per frame
     void Update()
     {
         death();
     }
 
     public void death()
     {
         if (currentHealth <= 0)
         {
             Destroy(gameObject);
         }
     }
 
     public void damageTaken(int attackdmg)
     {
         currentHealth -= attackdmg;
     }
 
 }
 

OppossumAttack

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class OpossumAttack : MonoBehaviour
 {
     public float timeattack = 1.5f;
     public int amount;
 
     GameObject PTarget;
     PlayerHealth PlayerHealth;
     bool playerRange;
     float timer;
 
     // Start is called before the first frame update
     void Start()
     {
         PTarget = GameObject.FindGameObjectWithTag("Player");
         PlayerHealth = PTarget.GetComponent<PlayerHealth>();
     }
 
     void OnCollisionEnter2D(Collision2D Opos)
     {
         if (Opos.gameObject == PTarget)
         {
             playerRange = true;
         }
     }
 
     void OnCollisionExit2D(Collision2D Opos)
     {
         if (Opos.gameObject == PTarget)
         {
             playerRange = false;
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         attackTime();
     }
 
 
     void attackTime()
     {
         timer += Time.deltaTime;
 
         if (timer >= timeattack && playerRange)
         {
             attack();
         }
 
         //if (PlayerHealth.currentHealth <= 0)
         //{
         //    if (PTarget.tag == "Player"){
         //     Destroy(gameObject, 2f);
         //}
         //}
     }
 
     void attack()
     {
         timer = 0f;
 
         if (PlayerHealth.currentHealth > 0)
         {
             PlayerHealth.TakenDmg(amount);
         }
     }
 
 }//opoAttack
 


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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Nikkq · Jun 06, 2019 at 11:26 AM

Hey mate I can't see your colliders but I suppouse there is one collider under your player character and at least one longer catched to the opossum. You do not define in your scripts whether you attack your enemy from top or bottom and that is why your enemy gets damage in first second of your video. Enemy probably gets inside collider when you jump and thats why he gets damage. I think you could use velocity direction to define if you jump on enemy from the top. The opossum script looks working fine.

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
avatar image
0

Answer by tormentoarmagedoom · Jun 06, 2019 at 11:24 AM

Hello this post is so long, so much code to read and unserstand..

What I can tell to you is: This kind of errors, that are not functions not working or giving erreos. This kind of errors of logic of your script (that comunicates and share information between them), must be solved by you.

You must debug your code while running, cheking values of all varialbles and finding the moment when something its not as you expect to be. Find where, and why.

But is almost imposible to detect it only by reading the code.

Good luck! Bye!

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

154 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

Related Questions

Object: collider to floor, trigger to player 1 Answer

Distance destroy object 3 Answers

Patrolling AI doesn't see player if it's standing still 1 Answer

About trigger 3 Answers

NavMesh problem it does not respond corectly 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