我是編程新手,我遇到了這個問題,每當我使用 Input.GetKeyDown(KeyCode.Mouse0) 攻擊我的角色時,它可能會觸發 10 次中的 3 次,我嘗試更改許多變數并嘗試將代碼在不同的地方,但我找不到修復它
這是代碼:
private void Update()
{
dirX = Input.GetAxisRaw("Horizontal") ;
rb.velocity = new Vector2(moveSpeed * dirX , rb.velocity.y) ;
if(Input.GetButtonDown("Jump") && (IsGrounded()))
{
rb.velocity = new Vector3(rb.velocity.x,jumpForce,0);
}
UpdateAnimationState();
if(attackTime <= 0)
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
anim.SetBool("attacking", true);
Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll( attackLocation.position, attackRange, enemies );
for (int i = 0; i < enemiesToDamage.Length; i )
{
enemiesToDamage[i].GetComponent<Enemy_health>().TakeDamage(damage);
}
}
attackTime = startTimeAttack;
}
else
{
attackTime -= Time.deltaTime;
anim.SetBool("attacking", false);
}
if(attackTime <= 0)
{
if( Input.GetKeyDown(KeyCode.Mouse0))
{
if(rb.velocity.y > .01f || rb.velocity.y < -.01f)
{
anim.SetBool("jumpattacking", true);
Collider2D[] damage = Physics2D.OverlapCircleAll( attackLocation.position, attackRange, enemies );
for (int i = 0; i < damage.Length; i )
{
Destroy( damage[i].gameObject );
}
}
}
attackTime = startTimeAttack;
}
else
{
attackTime -= Time.deltaTime;
anim.SetBool("jumpattacking", false);
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(attackLocation.position, attackRange);
}
uj5u.com熱心網友回復:
案件應該如何jumpattacking達成?
要么
attackTime高于0或第一種attacking情況已經開始并attackTime = startTimeAttack立即設定 -無論輸入如何!→ 你寧愿只有在你實際攻擊時才開始冷卻!
然后仍然
jumpattacking無法到達,因為attacking案例已經首先消耗了您的輸入并設定了計時器。最后,您使用影片器 Bool 引數的方式看起來像使用 Tigger 會更好,而不是使用Tigger一次更改為某個狀態并重置觸發器。
我不知道您的影片師是如何配置的,但可能是因為您立即在下一幀中重置 bool 引數,它會立即轉換回空閑狀態。
您可能應該做的是將兩種情況合并在一起,例如
private void Update()
{
dirX = Input.GetAxisRaw("Horizontal") ;
rb.velocity = new Vector2(moveSpeed * dirX , rb.velocity.y) ;
if(Input.GetButtonDown("Jump") && IsGrounded())
{
rb.velocity = new Vector3(rb.velocity.x, jumpForce, 0);
}
UpdateAnimationState();
if(attackTime <= 0)
{
if(Input.GetKeyDown(KeyCode.Mouse0))
{
// simply store this in a variable instead of implementing the same code twice
// Use the absolute to simply check the magnitude regardless of the sign
var isJumping = Mathf.Abs(rb.velocity.y) > 0.01f;
// Use ternary for switching between the two bools according to the isJumping
anim.SetBool(isJumping ? "jumpattacking" : "attacking", true);
// disable the according other bool (?)
// As said rather using Trigger would already solve this
//anim.SetBool(isJumping ? "attacking" : "jumpattacking", false);
var enemiesToDamage = Physics2D.OverlapCircleAll(attackLocation.position, attackRange, enemies);
foreach (var enemy in enemiesToDamage)
{
// again decide for the resulting action according to isJumping
if(isJumping)
{
Destroy(enemy.gameObject);
}
else
{
enemy.GetComponent<Enemy_health>().TakeDamage(damage);
}
}
// You only want to do this once you actually consumed the User input
attackTime = startTimeAttack;
}
}
else
{
attackTime -= Time.deltaTime;
// rather using Trigger parameters wold make these unnecessary
anim.SetBool("attacking", false);
anim.SetBool("jumpattacking", false);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/440944.html
