无法统一制作组合系统

问题描述 投票:0回答:1

我正在制作一款使用连锁攻击或组合系统的游戏。脚本附在下面

using StarterAssets;
using System.Collections;
using UnityEngine;
using UnityEngine.InputSystem;

public class SwordScript : MonoBehaviour
{
    public MonoBehaviour[] scriptsToDisable;
    public float attackRange = 10f;
    [Range(10f, 100f)] public float lightDamage = 10f;
    [Range(20f, 200f)] public float heavyDamage = 20f;
    public LayerMask EnemyLayerMask;


    Animator animator;
    StarterAssetsInputs inputs;
    InputActionMpa actionmap;
    bool isEquipped = false;
    ArcheryScript Archery;
    bool hasSword = false;
    bool isAttacking = false;

    int _chainCount = 0;
    float timeBetweenAttacks = 3f;
    float lastAttackTime = 0f;
    private void Awake()
    {
        animator = GetComponent<Animator>();
        inputs = GetComponent<StarterAssetsInputs>();
        Archery = GetComponent<ArcheryScript>();
        animator.SetLayerWeight(2, 0f);
    }

    private void OnEnable()
    {
        actionmap = new InputActionMpa();
        actionmap.Enable();
        actionmap.Player.EquipSword.performed += OnWithDrawSword;
    }

    private void OnDisable()
    {
        actionmap.Disable();
        actionmap.Player.EquipSword.performed -= OnWithDrawSword;
    }

    private void Update()
    {
        if (hasSword)//Sword Equip 
        {
            animator.SetLayerWeight(2, Mathf.Lerp(animator.GetLayerWeight(2), 1f, Time.deltaTime * 20f));
            animator.SetBool("EquipSowrd", true);
        }
        else
        {
            animator.SetBool("EquipSowrd", false);
            float delay = 1.5f;
            float timer = 0f;
            timer += Time.deltaTime;
            if (timer > delay)
            {
                animator.SetLayerWeight(2, Mathf.Lerp(animator.GetLayerWeight(2), 0f, Time.deltaTime * 10f));
            }
        }

        if (!isAttacking) // Attack System
        {

            if (inputs.shoot)
            {
                float currentTime = Time.time;
                if(currentTime - lastAttackTime < timeBetweenAttacks)
                {
                    _chainCount++; // chainCount = 1
                    
                }
                else
                {
                    _chainCount = 1;
                }
                lastAttackTime = currentTime;
            }

            if (_chainCount > 0)
            {
                StartCoroutine(PerformAttack("Light_Attack", _chainCount));
                _chainCount = 0; // Reset after starting the combo
            }

            int maxChainCount = 4;
            if (_chainCount > maxChainCount)
            {
                _chainCount = 1;
            }




        }
    }


    IEnumerator PerformAttack(string attackType, int chainCount)
    {
        isAttacking = true;
        animator.applyRootMotion = true;

        foreach (var script in scriptsToDisable) // enabling scripts  required after backstabbing
        {
            if (script != null)
            {
                script.enabled = false;
            }
        }
        
        for (int i = 0; i < chainCount; i++)
        {
            animator.SetTrigger(attackType + (i + 1));
            //Damage Logic

        }
        yield return new WaitForSeconds(2f);
        animator.applyRootMotion = false;
        isAttacking = false;

        foreach (var script in scriptsToDisable) // enabling scripts  required after backstabbing
        {
            if (script != null)
            {
                script.enabled = true;
            }
        }


    }
    private void OnWithDrawSword(InputAction.CallbackContext context) // check the isEquipped boolean
    {
        isEquipped = !isEquipped;
        bool checkBow = Archery.isEquippedBow;

        if (!checkBow)
        {
            if (isEquipped)
            {
                hasSword = true;
            }
            else
            {
                hasSword = false;
            }

        }

    }
}

问题是有一个参数

_chainCount
与链式攻击有关,但是
_chainCount
永远不会增加超过1,请帮我修复它....

我们将非常感谢您的帮助,并感谢您查看此内容。

c# unity-game-engine unityscript
1个回答
0
投票
    if (inputs.shoot)
    {
        float currentTime = Time.time;
        if(currentTime - lastAttackTime < timeBetweenAttacks)
        {
            _chainCount++; // chainCount = 1
            
        }
        else
        {
            _chainCount = 1;
        }
        lastAttackTime = currentTime;
    }

    if (_chainCount > 0)
    {
        StartCoroutine(PerformAttack("Light_Attack", _chainCount));
        _chainCount = 0; // This line causes the problem
    }

每当将计数器设置为 1 时,都会重置计数器,因此它不会超过 1。

© www.soinside.com 2019 - 2024. All rights reserved.