简体   繁体   English

团结角色不会停止冲刺

[英]Unity character doesn't stop sprinting

I only recently got into Unity, and I have made a character movement system;最近才接触Unity,做了一个角色移动系统; everything is working besides from sprinting.除了冲刺之外,一切都在工作。 I have the user use "Left Shift" to sprint, although it seems sprinting never turns off.我让用户使用“左移”来冲刺,尽管冲刺似乎永远不会关闭。 I'm not quite sure the reasoning as to why this happens or what part of code could be fixed to accommodate for this, as such, I have attached the source file of movement.我不太确定为什么会发生这种情况的原因,或者可以修复哪些代码部分来适应这种情况,因此,我附上了移动的源文件。

Here is the paste: https://pastebin.com/S1h5pEwq这是粘贴: https://pastebin.com/S1h5pEwq

In particular, this is the movement function:特别是,这是机芯 function:

void Movement()
    {
        Vector2 axis = new Vector2(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal")) * walkSpeed;
        Vector3 forward = new Vector3(-Camera.main.transform.right.z, 0.0f, Camera.main.transform.right.x);
        Vector3 wishDirection = (forward * axis.x + Camera.main.transform.right * axis.y + Vector3.up * rb.velocity.y);
        rb.velocity = wishDirection;

        if (Input.GetKey(KeyCode.LeftShift))
        {
            isSprinting = true;
        }
        else
        {
            isSprinting = false;
        }

        if (isSprinting == true)
        {
            walkSpeed = 10.0f;
        }
    }

More specifically, my goal is to have Sprint be activated strictly while the key is pressed.更具体地说,我的目标是在按下键时严格激活 Sprint。

Try this:尝试这个:

void Movement()
{
    Vector2 axis = new Vector2(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal")) * walkSpeed;
    Vector3 forward = new Vector3(-Camera.main.transform.right.z, 0.0f, Camera.main.transform.right.x);
    Vector3 wishDirection = (forward * axis.x + Camera.main.transform.right * axis.y + Vector3.up * rb.velocity.y);
    rb.velocity = wishDirection;

    if (Input.GetKey(KeyCode.LeftShift))
    {
        isSprinting = true;
    }
    else
    {
        isSprinting = false;
    }

    if (isSprinting == true)
    {
        walkSpeed = 10.0f;
    }
    else
    {
       walkSpeed = 0.0f
    }

}

The problem in your script was that you were not resetting the speed.您的脚本中的问题是您没有重置速度。

If(Input.GetKeyUp(KeyCode.LeftShift)){
isSprinting = false;
}

This will do, you didn't check when the player stopped pressing shift.这样就可以了,您没有检查播放器何时停止按 shift。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM