So I’m just making a basic fighter game (mortal kombat esk) and my attack only registers on the second hit, whether it’s on or off an enemy.
This is the code put on the hitbox for the attack:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player1AttackOne : MonoBehaviour
{
public bool attackOneHit = false;
// Start is called before the first frame update
void OnCollisionEnter2D(Collision2D collision2D)
{
attackOneHit = true;
}
}
This is the code for the combat:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerCombat2D : MonoBehaviour
{
public Animator animator;
public Player1AttackOne player1AttackOneScript;
void Update()
{
if (Input.GetMouseButtonDown(0))
{
AttackOne();
}
}
void AttackOne()
{
//Play AttackOne animation
animator.SetTrigger("Attack_One");
//Detect other players in range of AttackOne hitbox
if (player1AttackOneScript.attackOneHit == true)
{
Debug.Log("Passed to PlayerCombat2D");
player1AttackOneScript.attackOneHit = false;
}
}
}