-当我输入前3次攻击时,第3次动画只会在我点击第4次后播放 - 当我垃圾点击时,第一个动画只会在前 3 个垃圾点击后播放,在第 3 个动画完成后
任何帮助或建议?
public class swordscript : MonoBehaviour
{
public GameObject Sword;
private Animator anim;
private int comboStep = 0; // Current step in the combo
private float lastComboTime = 0f; // Time when the last attack was started
private float comboTimeout = 1.0f; // Time window for completing the combo
private float ResetAtkCooldown = 0.5f;
void Start()
{
// Get the Animator component from the Sword GameObject
anim = Sword.GetComponent<Animator>();
}
void Update()
{
// Check if the player has clicked the mouse
if (Input.GetMouseButtonDown(0))
{
// Check if we're in a combo
if (Time.time - lastComboTime <= comboTimeout)
{
// Increment the combo step
comboStep++;
// Perform the appropriate action based on the combo step
switch (comboStep)
{
case 1:
anim.SetTrigger("Attack");
StartCoroutine(ResetToIdle());
break;
case 2:
anim.SetTrigger("Attack2");
StartCoroutine(ResetToIdle());
break;
case 3:
anim.SetTrigger("Attack3");
StartCoroutine(ResetToIdle());
break;
default:
// Reset the combo if the player takes too long to complete it
if (comboStep > 3)
{
comboStep = 1;
}
break;
}
}
else
{
// Start a new combo
comboStep = 1;
}
// Record the time of the last attack
lastComboTime = Time.time;
}
}
IEnumerator ResetToIdle()
{
yield return new WaitForSeconds(ResetAtkCooldown);
comboStep = 0;
}
}
我希望在我输入的每次点击中播放动画攻击(比如第一次点击 = 攻击,第二次点击 = 攻击 2,第三次点击 = 攻击 2,并且当我再点击 3 次时将在攻击字符串后再次重置)
此代码存在许多问题,您最好放置一个调试器,以查看您的代码实际如何与您的预期行为分开。例如:
那个
// Check if we're in a combo
if (Time.time - lastComboTime <= comboTimeout) {
// Increment the combo step
comboStep++;
像这样的状态控制块,我推荐State Design Pattern。我从这个很容易理解的博客中学到: https://gameprogrammingpatterns.com/state.html
祝你好运
注意:您可以散列触发字符串以获得更清晰的代码和更好的性能
private static readonly int attack = Animator.StringToHash("Attack");