Swich weapon on selected action changed

If we add that listener to UnitActionSystem.Instance.OnSelectedActionChanged Unit will change weapon before starting action:

UnitAnimator:

    [SerializeField] private Animator _animator;
    [SerializeField] private Transform _bulletProjectilePrefab;
    [SerializeField] private Transform _rifleHandler;
    [SerializeField] private Transform _swordHandler;

    private Unit _unit;
    private MoveAction _moveAction;
    private ShootAction _shootAction;
    private SwordAction _swordAction;

    private void Awake()
    {
        _unit = GetComponentInChildren<Unit>();

        if (TryGetComponent(out _moveAction))
        {
            _moveAction.OnStartMoving += OnStartMoving;
            _moveAction.OnStopMoving += OnStopMoving;
        }
        if (TryGetComponent(out _shootAction))
            _shootAction.OnShoot += OnShoot;
        if (TryGetComponent(out _swordAction))
            _swordAction.OnActionStarted += OnSwardActionStarted;

        UnitActionSystem.Instance.OnSelectedActionChanged += OnSelectedActionChanged;     
    }

    private void OnSelectedActionChanged(object sender, EventArgs args)
    {
        if (sender is UnitActionSystem uas && uas.SelectedUnit == _unit)
        {
            bool isSwordActionSelected = uas.SelectedAction is SwordAction;
            _rifleHandler.gameObject.SetActive(!isSwordActionSelected);
            _swordHandler.gameObject.SetActive(isSwordActionSelected);
        }
    }

image
image


3 Likes

Good idea! However, in that case maybe we should also listen to OnSelectedActionChanged in the animator in order to switch to another idle animation when the unit have a sword in its hand.

Privacy & Terms