Third Person Combat Course Issues, Part 1/3

OK so I have 3 major issues (4, with the ledge problem) in my Unity 3rd Person Course content. Here is the first one:

  1. I can’t strike my enemy for some reason (i.e: I can’t deal damage), although my weapon has a box collider on it

Getting a screenshot of this one specifically is quite hard (even if I try inserting a TimeScale freeze code, it’s still a hard catch to get), but long story short, I attempted to slow down the speed of my enemys’ attacks to catch my player attacking him, and as this is connected to my third problem here (my issue regarding the fact that my animations don’t deal their assigned damages), my player basically deals absolutely no damage towards my enemy through the first animations, although when I previously managed to catch my player striking my enemy in the past, the colliders were close enough to count this as a strike, so I figured something was completely off.

Where did I go wrong?

Bahaa, Just a couple of friendly suggestions. It would help Brian and others if you posted more information in the first post. You know that they are going to ask for the relevant scripts and screen shots of the inspector. Try to save Brian some time by giving more information up front. Also, make the topic title more descriptive. After each of these get longer, it will be harder to keep them straight. Brian is a great resource, lets make it easier for him to help you.

1 Like

Apologies man, I was literally travelling between countries the entire morning today. I updated my posts to the best of my abilities. Unfortunately for this specific problem, a screenshot was not easily available, so I did my absolute best to describe and link the issue to the third part of my problems. I hope this clarifies it :slight_smile:

Most often, this issue boils down to one of three things:

  • The collider on the weapon never makes contact with the opponent’s collider because of distance
  • The collider isn’t properly being activated/deactivated by the Animation Event (in other words, the name of the event to activate doesn’t match the name of the method in WeaponHandler.
  • The AlreadyCollidedWith list isn’t being reset in WeaponDamage

The first one is the most common. What I did for this is extend the size of my collider on the weapon so that it actually reaches a meter or so past the end of the weapon. It’s often hard to actually get the characters close enough together to do damage. You can test if the colliders are triggering by adding a Debug.Log to the WeaponDamage.OnTriggerEnter() method (after filtering out collisions with yourself.

The second one is also fairly common. If the animation event isn’t getting handled, you’ll get a message saying “{animation name} has no receiver” (or something along those lines.

For the third one, make sure that every time WeaponDamage is Enabled, the AlreadyCollidedWith List is cleared.

I don’t think this is the issue (my enemy’s collider is shorter than my main players’ collider, and it works perfectly fine)

Toggle looks fine to me (Investigated it through gameplay, seems fine)

I think this is the one. I turned my ‘alreadyCollidedWith’ to public to investigate what it’s getting during playtime, and… it gets nothing. The list remains pure zero, with no updates whatsoever.
Here is my ‘WeaponDamage.cs’, if that helps:

using System.Collections.Generic;
using UnityEditor.Experimental.GraphView;
using UnityEngine;

public class WeaponDamage : MonoBehaviour
{

    [SerializeField] private Collider myCollider;

    // REVERSE THESE THREE VARIABLES TO PRIVATE:
    public List<Collider> alreadyCollidedWith = new List<Collider>();
    public int damage;
    public float knockback;

    private void OnEnable() {
        alreadyCollidedWith.Clear();
    }

    private void OnTriggerEnter(Collider other) {
        
        if (other == myCollider) return;

        if (alreadyCollidedWith.Contains(other)) return;
        alreadyCollidedWith.Add(other);

        if (other.TryGetComponent<Health>(out Health health)) {
            health.DealDamage(damage);
        }

        if (other.TryGetComponent<ForceReceiver>(out ForceReceiver forceReceiver)) {
            Vector3 direction = (other.transform.position - myCollider.transform.position).normalized;
            Vector3 force = direction * knockback;
            Debug.Log($"Applying {force} force to {forceReceiver.gameObject.name}");
            forceReceiver.AddForce(force);
        }

    }

    public void SetAttack(int damage, float knockback) {
        this.damage = damage;
        this.knockback = knockback;
        Debug.Log($"Setting {myCollider.gameObject.name}'s Weapon Damage on {name}. Damage = {damage}, knockback = {knockback}");
    }

}

If your alreadyCollidedWith isn’t getting added to, then no hit has occurred.

Privacy & Terms