冲刺不会工作,除非我关闭播放最大化和 select 我的播放器

sprinting wont work unless I turn off maximize on play and select my player

我正在尝试制作一个 2d 平台游戏,很久以前我添加了短跑并且它运行得很好。昨天它随机停止工作,我不知道为什么所以我试图通过关闭播放最大化并在按住shift时选择我的播放器来测试当我按住shift时我的速度和当前耐力值是否发生变化。当我测试它时,值正在改变并且由于某种原因我的冲刺正在运行,但是当我停止选择我的播放器并选择其他东西时,它停止让我冲刺。我不认为这是我的代码的错误我只是认为这是一个统一故障但我还是会包含我的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerContoller : MonoBehaviour
{
    private Rigidbody2D rb;
    public float speed = 7.34f;
    public float jumpForce;
    private float moveInput;

    private bool isGrounded;
    public Transform feetPos;
    public float checkRadius;
    public LayerMask whatIsGround;
    public LayerMask Slow;

    private float jumpTimeCounter;
    public float jumpTime;
    private bool isJumping;

    public int maxStamina = 163;
    public int currentStamina;
    public bool canSprint;

    public Stamina stamina;

    private Animator anim;

    void Start()
    {
        anim = GetComponent<Animator>();
        rb = GetComponent<Rigidbody2D>();

        currentStamina = maxStamina;
        stamina.SetMaxStamina(maxStamina);
    }

    private void FixedUpdate()
    {
        moveInput = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

        if(moveInput == 0)
        {
            anim.SetBool("isRunning", false);
        } else
        {
            anim.SetBool("isRunning", true);
        }

        if (currentStamina > 0)
        {
            canSprint = true;
        } else if (currentStamina < 0)
        {
            canSprint = false;
        }
        if (currentStamina < maxStamina)
        {
            RegenStamina(2);
        }


        if (canSprint == true)
        {
            if (Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.LeftShift))
            {
                LoseStamina(5);
                speed = 14f;
            }
        } else
        {
            speed = 7.34f;
        }

        if (canSprint == false)
        {
            speed = 7.34f;
        }
    }

    private void Update()
    {
        isGrounded = Physics2D.OverlapCircle(feetPos.position, checkRadius, whatIsGround);

        if (moveInput > 0)
        {
            transform.eulerAngles = new Vector3(0, 0, 0);
        }
        else if (moveInput < 0)
        {
            transform.eulerAngles = new Vector3(0, 180, 0);
        }

        if (isGrounded == true && Input.GetKeyDown(KeyCode.Space))
        {
            anim.SetTrigger("takeOff");
            isJumping = true;
            jumpTimeCounter = jumpTime;
            rb.velocity = Vector2.up * jumpForce;
            
        }

        if (isGrounded == true)
        {
            anim.SetBool("isJumping", false);
        }
        else
        {
            anim.SetBool("isJumping", true);
        }

        if(Input.GetKey(KeyCode.Space) && isJumping == true)
        {
            if(jumpTimeCounter > 0)
            {
                rb.velocity = Vector2.up * jumpForce;
                jumpTimeCounter -= Time.deltaTime;
            }
            else
            {
                isJumping = false;
            }

        }

        if (Input.GetKeyUp(KeyCode.Space))
        {
            isJumping = false;
        }
    }

    void LoseStamina(int StaminaGone)
    {
        currentStamina -= StaminaGone;

        stamina.SetStamina(currentStamina);
    }

    void RegenStamina(int AddStamina)
    {
        currentStamina += AddStamina;

        stamina.SetStamina(currentStamina);
    }
}

在不同的脚本中我有

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Stamina : MonoBehaviour
{
    public Slider slider;

    public void SetMaxStamina(int Stamina)
    {
        slider.maxValue = Stamina;
        slider.value = Stamina;
    }

    public void SetStamina(int Stamina)
    {
        slider.value = Stamina;
    }
}

(这是为了让滑块移动)

因为你的最大耐力是 163,并且你每 FixedUpdate 减少 5 耐力,所以你很可能不会在足够长的时间内体验到冲刺效果(~0.4 秒),假设你没有更改默认的修复更新率 0.02(每秒 50 次更新)。 您可以尝试增加体力值进行测试或减少每次执行FixedUpdate方法的体力消耗。

如果您想在 FixedUpdate 中执行此操作,请尝试以下操作:

LoseStamina(5 * Time.fixedDeltaTime);

这会将体力消耗更改为每秒 5 点,您可以根据需要进行调整。