Weapon Hit Detection

Hey, i am trying to make a 3D game and i want to make a melee combat system, I tried to use the OnTriggerEnter function but it is not working, it passes through the enemy (which as a rigidbody) and doesnt register the hit, if I tick the Is Trigger in the enemy mesh collider the enemy just falls off the map, i dont know what to do…

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

    public class DamageColider : MonoBehaviour
    {
        [SerializeField]
        Collider damageCollider;
        public int weaponDamage = 10;

        private void Awake()
        {
            damageCollider.gameObject.SetActive(true);
            damageCollider.isTrigger = true;
            damageCollider.enabled = false;
        }

        public void EnableDamageCollider()
        {
            damageCollider.enabled = true;
        }

        public void DisableDamageCollider()
        {
            damageCollider.enabled = false;
        }

        public void OnTriggerEnter(Collider other)
        {
            Debug.Log("Hit: " + other.name);

            if (other.tag == "Player")
            {
                other.GetComponent<PlayerStats>().TakeDamage(weaponDamage);
            }

            if (other.tag == "Enemy")
            {
                other.GetComponent<EnemyStats>().TakeDamageEnemy(weaponDamage);
            }
        }
    }
1 Like

If you are using a melee system, you can try implementing it with Physics.Raycast.