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 Robb_oates · Aug 09, 2021 at 12:21 PM · prefabdamage

2 Prefabs are fighting each other but both are taking damage when one is hit.

Im making an Gladiator Manager Game but when 2 gladiator are fighting they both take damage when 1 get hit. they are both a clone of the same prefab and im dealing with damage with a trigger collider to the send damage through a "getcomponent" command.

Here is the code:

 public int state;
 private Animator anim;
 private Rigidbody2D rb;
 private GameObject closestTarget;

 [Header("Movement Stats")]
 public float moveSpeed;
 public float jumpForce;
 public float dist;

 private float dir;
 private float dashTimer;

 [Header("Character Stats")]
 [Range(1,10)]
 public float stamina;
 public float attackRange;
 [Range(0,10)]
 public float attackSpeed;
 [Range(0,10)]
 public float dodgeChance;
 private float damage;
 private float attackTimer;
 private float attackNumber;
 [HideInInspector]
 public bool isAttacking;

 [Header("Attack01 Stats")]
 public float attack01Min;
 public float attack01Max;

 [Header("Attack02 Stats")]
 public float attack02Min;
 public float attack02Max;

 [Header("Attack03 Stats")]
 public float attack03Min;
 public float attack03Max;



 private void Start()
 {
     health = gameObject.GetComponent<HealthAndDamage>();
     rb = gameObject.GetComponent<Rigidbody2D>();
     anim = gameObject.GetComponent<Animator>();
     dir = 1;
     state = Random.Range(1,2);
 }

 private void FixedUpdate()
 {
     TargetFinder();
     VariableUpdate();
     if(state == 1)
     {
         State1();
     }else if(state == 2)
     {
         State2();
     }
 }

 public void Attack()
 {
     if (attackTimer <= 0)
     {
         if (attackNumber < 0.35f)
         {
             damage = Random.Range(attack01Min, attack01Max);
             anim.SetTrigger("Attack01");
             isAttacking = true;
             attackTimer = 10;
         }
         else
         {
             damage = Random.Range(attack02Min, attack02Max);
             anim.SetTrigger("Attack02");
             isAttacking = true;
             attackTimer = 10;
         }
     }
 }

 void State1()
 {
     Debug.Log("State 1 Called");
     if (dashTimer > 0)
     {
         dashTimer -= 1 * Time.deltaTime;
     }

     if (dist > attackRange)
     {
         anim.SetBool("isMoving", true);
         transform.position = new Vector2(transform.position.x + moveSpeed * dir * Time.deltaTime, transform.position.y);
     }
     else
     {
         anim.SetBool("isMoving", false);
         if (Random.Range(0f, 1f) < 0.75f)
         {
             Debug.Log("Attack Called");
             Attack();
         }
     }
 }

 void State2()
 {
     if (dashTimer <= 0 && closestTarget.GetComponent<GladiatorAI>().isAttacking == true)
     {
         if (Random.Range(0f, 1.2f) < dodgeChance / 10)
         {
             Debug.Log("Dash Called");
             anim.SetTrigger("DashBack");
             Vector2 force = new Vector2(-6.5f * dir, 0.25f);
             rb.AddForce(force, ForceMode2D.Impulse);
             dashTimer = 3;
         }
     }
     else
     {
         Attack();
     }
 }


 void TargetFinder()
 {
     GameObject[] gos = GameObject.FindGameObjectsWithTag("Gladiator");
     foreach (GameObject go in gos)
     {
         if (go != gameObject)
         {
             enemyHealth = go.GetComponent<HealthAndDamage>();
             if (closestTarget == null)
             {
                 closestTarget = go;
             }
             else if (Vector2.Distance(gameObject.transform.position, go.transform.position) < dist)
             {
                 closestTarget = go;
             }
         }
     }
 }

 void VariableUpdate()
 {
     if (closestTarget != null)
     {
         dist = Vector2.Distance(gameObject.transform.position, closestTarget.transform.position);
     }

     if(gameObject.transform.position.x - closestTarget.transform.position.x > 0)
     {
         dir = -1;
         transform.rotation = new Quaternion(0, 180, 0, 0);
     }
     else
     {
         dir = 1;
         transform.rotation = new Quaternion(0, 0, 0, 0);
     }

     attackTimer -= attackSpeed * Time.deltaTime;
     attackNumber = Random.Range(0.01f, 1);
     
 }
 private void OnTriggerEnter2D(Collider2D collision)
 {
     Debug.Log(collision.gameObject.name);
     if(collision != this.gameObject)
     {
         collision.gameObject.GetComponent<GladiatorAI>().Hurt();
         collision.gameObject.GetComponent<HealthAndDamage>().health -= damage;
         damage = 0;
     }
 }

 public void Hurt()
 {
     anim.SetTrigger("Hurt");
 }

 public void BlockFinished()
 {

 }

 public void AttackFinished()
 {
     isAttacking = false;
 }

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 berkun5 · Aug 09, 2021 at 06:02 PM

  private void OnTriggerEnter2D(Collider2D collision)
  {
      Debug.Log(collision.gameObject.name);
      if(collision != this.gameObject)
      {
          collision.gameObject.GetComponent<GladiatorAI>().Hurt();
          collision.gameObject.GetComponent<HealthAndDamage>().health -= damage;
          damage = 0;
      }
  }

Seems like they both touching each other and damaging the other one. Maybe you could use an additional condition to the OnTriggerEnter2D. For example, if one of the gladiator has "isAttacking" animation active. Since the attacker also got touched by the defender one right now, I guess it causes you this problem.

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

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

Related Questions

Script for enemy health loss on contact with bullet prefab 1 Answer

Can't manage to damage enemies... 1 Answer

How to have an NPC with multiple parts detect damage 1 Answer

Help making a damage number system 1 Answer

Collision Damage, Hit points and Instantiate 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