unity3d 不明白为什么我的破折号在Unity2D中不起作用?

f87krz0w  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(158)

我在Unity上创建一个2D平台,我是一个初学者。我的角色可以向左和向右,并跳跃(目前没有双跳)。我想让他冲刺时,我按下左shift +箭头键的方向,他去。我在youtube上看了几个图图,并选择了一个灵感来自HollowKnight和塞莱斯特。
问题是当我试图在跑步时冲刺时,它不起作用,角色只是飞/飘走,而不是只是冲刺(即使我不按跳跃触摸)。
希望我的职位是足够的欠稳定,我是法国人,所以让我知道,如果你需要一些精度.我很肯定我有一些行,在代码中没有任何关系,但因为我不明白我读的所有,我尝试了几件事,所以我的代码看起来像弗兰肯斯坦...请,善良
这是我的代码:

public class DashHollowKnight : MonoBehaviour
{
    [Header("Dashing")]
    [SerializeField]
    private float dashingVelocity = 14f;

    [SerializeField]
    private float dashingTime = 0.5f;

    private Vector2 dashingDir;
    private bool isDashing;
    private bool canDash = true;

    public float speed;
    public float moveInput;

    private bool isOnGround = true;
    public Animator animator;

    public Rigidbody2D playerRb;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    private void OnCollisionEnter2D(Collision2D col)
    {
        isOnGround = true;
    }

    // Update is called once per frame
    void Update()
    {
        var dashInput = Input.GetButtonDown("Dash");

        if (dashInput && canDash)
        {
            isDashing = true;
            canDash = false;
            dashingDir = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
            if (dashingDir == Vector2.zero)
            {
                dashingDir = new Vector2(transform.localScale.x, 0);
            }
            StartCoroutine(StopDashing());

        }

        animator.SetBool("IsDashing", isDashing);

        if (isDashing)
        {
            playerRb.velocity = dashingDir.normalized * dashingVelocity;
            if (dashingTime <= 0)
                playerRb.velocity = new Vector2(moveInput * speed, playerRb.velocity.y);
            return;
        }

        
    }

    private IEnumerator StopDashing()
    {
        yield return new WaitForSeconds(dashingTime);
        isDashing = false;
        canDash = true;
    }
}

我在youtube上关注了几个tutos,我选择了lokks喜欢最适合我需要的那个。
我完全按照视频所说的,并搜索什么可以使它不工作,但我卡住了。

oug3syen

oug3syen1#

尝试更改playerRb.velocity轴:

if (isDashing)
{
    playerRb.velocity = dashingDir.normalized * dashingVelocity;
    if (dashingTime <= 0)
        // you have - velocity is added to the y axis (vertical)
        playerRb.velocity = new Vector2(moveInput * speed, playerRb.velocity.y);

        // change to - velocity is added to the x axis (horizontal)
        playerRb.velocity = new Vector2(moveInput * speed, playerRb.velocity.x);
     return;
}

相关问题