我开始了我的第一个游戏项目,我坚持我的破折号
我的破折号随机开始工作时,我按下破折号按钮(R),但它并不总是工作。我是新的游戏开发,并试图寻找解决方案-像固定更新或添加力量或转换。位置-但没有帮助。如果你有任何想法,你能帮助我吗?
我代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rb;
private BoxCollider2D coll;
private int jumpCount = 1;
private bool canDash = true;
private bool isDashing;
[SerializeField] private LayerMask jumpableGround;
// Start is called before the first frame update
private void Start()
{
rb = GetComponent<Rigidbody2D>();
coll = GetComponent<BoxCollider2D>();
}
// Update is called once per frame
private void Update()
{
float dirX = Input.GetAxisRaw("Horizontal");
rb.velocity = new Vector2(dirX * 7f, rb.velocity.y);
if (Input.GetButtonDown("Jump") && jumpCount != 0)
{
rb.velocity = new Vector2(rb.velocity.x, 12f);
jumpCount--;
}
if (IsGrounded())
{
jumpCount = 1;
}
if(Input.GetKeyDown(KeyCode.R) && canDash)
{
_ = StartCoroutine(Dash());
}
}
private bool IsGrounded()
{
return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
}
private IEnumerator Dash()
{
canDash = false;
float originalGravity = rb.gravityScale;
rb.gravityScale = 0f;
rb.velocity = new Vector2(rb.velocity.x, 0f);
rb.AddForce(new Vector2(rb.velocity.x * 24f, 0f), ForceMode2D.Impulse);
Debug.Log("something");
rb.gravityScale = originalGravity;
yield return new WaitForSeconds(0.1f);
canDash = true;
}
}
1条答案
按热度按时间pkwftd7m1#
看起来您正在覆盖每次调用Update()时的速度:
您可以为“_isDashing”添加一个字段,如果用户正在冲刺,则不要覆盖速度。
如果你没有完全使用内置的物理引擎,你可能还需要跟踪破折号的方向。例如,一些逻辑像“如果不破折号,更新速度为正常。如果破折号,更新速度和破折号方向为适当的设置”。