Unity RPG 2D Course Sword Collision - Weapon Collider SetActive not working

I am currently doing the Unity RPG 2D Course and have come across a problem that prevents me from continuing. In the Sword Collision video, we activate and deactivate the Weapon Collider as you slash the sword. I have checked and rechecked my code and settings, as well as trying a solution posted in the comments, but once the Weapon Collider’s SetActive is set to false, it stays deactivated. I even copied and pasted the code from the repository and it stays the same. Can anyone help?

Let’s start by taking a look at your Sword.cs script.

Here’s the code for Sword.cs, it’s basically the same as the repository as I copied and pasted the code from there to check. I can also see in the hierarchy that my Weapon Collider stays deactivated after the first time:

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

public class Sword : MonoBehaviour
{
    [SerializeField] private GameObject slashAnimPrefab;
    [SerializeField] private Transform slashAnimSpawnPoint;
    [SerializeField] private Transform weaponCollider;

    private PlayerControls playerControls;
    private Animator myAnimator;
    private PlayerController playerController;
    private ActiveWeapon activeWeapon;

    private GameObject slashAnim;

    private void Awake() {
        playerController = GetComponentInParent<PlayerController>();
        activeWeapon = GetComponentInParent<ActiveWeapon>();
        myAnimator = GetComponent<Animator>();
        playerControls = new PlayerControls();
    }

    private void OnEnable() {
        playerControls.Enable();
    }

    void Start()
    {
        playerControls.Combat.Attack.started += _ => Attack();
    }

    private void Update() {
        MouseFollowWithOffset();
    }

    private void Attack() {
        myAnimator.SetTrigger("Attack");
        weaponCollider.gameObject.SetActive(true);

        slashAnim = Instantiate(slashAnimPrefab, slashAnimSpawnPoint.position, Quaternion.identity);
        slashAnim.transform.parent = this.transform.parent;
    }

    public void DoneAttackingAnimEvent() {
        weaponCollider.gameObject.SetActive(false);
    }

    public void SwingUpFlipAnimEvent() {
        slashAnim.gameObject.transform.rotation = Quaternion.Euler(-180, 0, 0);

        if (playerController.FacingLeft) { 
            slashAnim.GetComponent<SpriteRenderer>().flipX = true;
        }
    }

    public void SwingDownFlipAnimEvent() {
        slashAnim.gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);

        if (playerController.FacingLeft)
        {
            slashAnim.GetComponent<SpriteRenderer>().flipX = true;
        }
    }

    private void MouseFollowWithOffset() {
        Vector3 mousePos = Input.mousePosition;
        Vector3 playerScreenPoint = Camera.main.WorldToScreenPoint(playerController.transform.position);

        float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;

        if (mousePos.x < playerScreenPoint.x) {
            activeWeapon.transform.rotation = Quaternion.Euler(0, -180, angle);
            weaponCollider.transform.rotation = Quaternion.Euler(0, -180, 0);
        } else {
            activeWeapon.transform.rotation = Quaternion.Euler(0, 0, angle);
            weaponCollider.transform.rotation = Quaternion.Euler(0, 0, 0);
        }
    }
}

I think I found the problem, and it was so obscure I’m just happy to have spotted it when I did. For some reason, the animation event that triggered the DoneAttackingAnimEvent duplicated right next to the SwingUp and SwingDown animation events, and I didn’t see it because it was a tiny little white mark right next to the events. So it turned the collider on and immediately off again. I deleted those and it seems to work again! Such a tiny issue but took three days to debug.

Those are some of the hardest things to debug. One thing that can be very frustrating is that if two events are very close, it’s hard to see that there is an extra event, or to actually select the other event.

1 Like

I thought it had something to do with the animation, because I tried to enable/disable the object through code and that worked, but through an animation event it wouldn’t. Yet the event triggered the rest of the code so it just didn’t make any sense. Somehow an extra DoneAttackingAnimEvent sneaked in, I must have done something like copy/paste or who knows what, but being new to animation events I had no idea that the tiny little white mark was another event :upside_down_face:

I tend to log out Animation Events as I’m getting things set up. If you include a timestamp to a Debug.Log on each Animation Event handler, then you might see something like

SwingUp 1:00:00
DoneAttackingAnimEvent 1:00:00
DoneAttackingAnimEvent 1:00:05
1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms