koki
koki

Reputation: 39

Physics OverlapSphere does not detect Collision

I wanted to create an collider on my players sword, that if he attacks that he would detect and via an animation event turn off/on an gameobject called (Damage Point) who was an script attached that would subtract the enemys health. But somewhere it does not detect correctly. I tried to add the OnDrawGizmos function to see my sphere collider but even after making it bigger it does not detect.

screenshot 1

The most strange thing about my issue is that im using the same code for my monster chest and my fantasy player, but for the chest it works but for the player it does not.

I created a class called PlayerDamage that is attached on an empty gameobject to the tip of my sword.

{
        public class PlayerDamage : MonoBehaviour
        public int damageAmount = 2;
        public LayerMask enemyLayer;
    
        void Update()
        {
            Collider[] hits = Physics.OverlapSphere(transform.position, 1.7f, enemyLayer);
            
    
            if (hits.Length > 0)
            {
                if (hits[0].gameObject.tag == MyTags.ENEMY_TAG)
                {
                    print("COLLIDED WITH ENEMY");
                }
            }
        }
    
        private void OnDrawGizmos()
        {
            Gizmos.DrawWireSphere(transform.position, 1.7f);
        }

screenshot 2

In another script called PlayerScript that is attached directly to the player I have a function called Attack:

void Attack()
    {
        if (Input.GetKeyDown(KeyCode.K))
        {
            if (!anim.GetCurrentAnimatorStateInfo(0).IsName(MyTags.ATTACK_ANIMATION) || !anim.GetCurrentAnimatorStateInfo(0).IsName(MyTags.RUN_ATTACK_ANIMATION))
            {
                anim.SetTrigger(MyTags.ATTACK_TRIGGER);
            }
        }
    }

screenshot 3

Also in the PlayerScript class there are two functions called ActivateDamagePoint and DeactivateDamagePoint, these are assigned to animation events for the attack animations.

void ActivateDamagePoint()
    {
        damagePoint.SetActive(true);
    }

    void DeactivateDamagePoint()
    {
        damagePoint.SetActive(false);
    }

screenshot 4

I double checked that everything is on his layer and that the tags are okay, but that did not solve my problem.

screenshot 5 screenshot 6

Like I said before for the chest I use the same code and it works, but unfortunately it does not work with my player. I also have the Activate and Deactivate DamagePoint functions in there and these are also called by the animation event for my chest attack animation.

Upvotes: 1

Views: 1551

Answers (1)

koki
koki

Reputation: 39

Okay after 1hour of debugging I finnaly found the solution:

It was in the MyTags class (a helper class for all the string variables), the error was:

public static string ENEMY_TAG = "Enemey";

instead of:

public static string ENEMY_TAG = "Enemy"; 

Upvotes: 1

Related Questions