- Home /
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
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.