【问题标题】:UNITY - Attack Hit Combo Code Error. Help neededUNITY - 攻击命中组合代码错误。需要帮助
【发布时间】:2017-02-26 21:33:04
【问题描述】:

我的代码有问题。当我按下分配的键码“Z”时,CurrentState 不会更改为 1 并继续执行整个组合。我尝试放置一个 debug.log,发现盒子碰撞器在按下 Z 时会激活,但数字不会增加 1。有人可以帮帮我吗?这是代码。

公共类斗士:MonoBehaviour {

public Collider[] attackhitboxes;
public Animator art;
public int CurrentState = 0;
bool activateTimertoreset = false;
public float currentcombotimer;
float origTimer = 50f;
private void Start()
{
    art = gameObject.GetComponent<Animator>();
    origTimer = currentcombotimer;
}
void Update()
{
    art.SetInteger("currentstate", CurrentState);
    NewComboSystem();
    ResetComboState(activateTimertoreset);

}

void ResetComboState(bool resettimer)
{
    if(resettimer)
    {
        currentcombotimer -= Time.deltaTime;

        if(currentcombotimer <= 0)
        {
            CurrentState = 0;
            activateTimertoreset = false;
            currentcombotimer = origTimer;
        }
    }
}
    private void LaunchAttack(Collider col)
{
    Collider[] cols = Physics.OverlapBox(col.bounds.center, col.bounds.extents, col.transform.rotation, LayerMask.GetMask("Hitbox"));
    foreach(Collider c in cols)
    {
        if(c.transform.parent.parent == transform)
        {
            continue;
        }
    }
}

void NewComboSystem()
{
    if (Input.GetKeyDown(KeyCode.Z))
    {
        activateTimertoreset = true;
        CurrentState++;
        if (CurrentState == 1)
        {
            LaunchAttack(attackhitboxes[0]); 
        }

        if (CurrentState == 2)
        {
            LaunchAttack(attackhitboxes[0]);
        }

        if (CurrentState >= 3)
        {
            LaunchAttack(attackhitboxes[0]);
        }
    }
}

}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    尝试将你的 if (input.keydown) 放在更新函数中,让它调用你的攻击。以你拥有它的方式,我相信它每秒多次调用你的攻击函数,因为更新每帧都会运行。也尝试调试 currentstate 的值。

    编辑: 您是否在编辑器中为 currentcombotimer 分配一个值,因为它是公开的?在脚本中它没有起始值。在启动函数中,您将其分配给 origTimer。如果此值为零,则您的组合时间为零。

    【讨论】:

    • 不是问题,因为Input.GetKeyDownNewComboSystem() 函数中,该函数100% 被Update() 函数调用。
    • 是的,这似乎不是问题。我仍然继续尝试,但没有奏效。关于我的代码有什么问题的任何其他建议?
    • 您是否在编辑器中为 currentcombotimer 分配一个值,因为它是公开的?在脚本中它没有起始值。在启动函数中,您将其分配给 origTimer。如果此值为零,则您的组合时间为零。
    • 好的,我想通了,你帮我想通了下一部分。由于一些非常奇怪的原因,把 NewComboSystem();在更新中的 resettimer thingy 开始增加 currenstate 的值之后。在我这样做之后,我被卡住了,因为该值会增加 0 秒。我来到这里,看到你谈论 currentcombotimer 并继续修复它。现在一切都很好,它就像一个魅力。太感谢了。如果不是你,我会像个傻瓜一样在我的代码中挣扎,试图找出问题所在。
    • 这是个好消息。
    猜你喜欢
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多