Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在不重写变量的情况下声明变量时出现问题_C#_Unity3d - Fatal编程技术网

C# 在不重写变量的情况下声明变量时出现问题

C# 在不重写变量的情况下声明变量时出现问题,c#,unity3d,C#,Unity3d,在函数内部,我必须声明两个局部浮点变量:jAxis和jumpValue。jAxis在开始时立即被分配一个变量,而jumpValue只是浮点jumpValue 我在代码后面有一个if语句,其中使用了jumpValue。但是,在到达该点之前,决不能取消为jumpValue分配值。编译器不同意在底部附近的Vector3计算中使用未分配的局部变量jumpValue的错误 这是我的相关代码: void JumpHandler() { float jAxis = Input.GetAxis("Ju

在函数内部,我必须声明两个局部浮点变量:jAxis和jumpValue。jAxis在开始时立即被分配一个变量,而jumpValue只是浮点jumpValue

我在代码后面有一个if语句,其中使用了jumpValue。但是,在到达该点之前,决不能取消为jumpValue分配值。编译器不同意在底部附近的Vector3计算中使用未分配的局部变量jumpValue的错误

这是我的相关代码:

void JumpHandler()
{

    float jAxis = Input.GetAxis("Jump");
    float jumpValue;

    //if pressed
    if (jAxis > 0)
    {
        bool isGrounded = CheckGrounded();

        if (isGrounded)
        {
            jumpValue = jAxis; 
            if (!jumpPressed)
            {
                jumpPressed = true; //Pressed jump
            }
        }
        else
        {
            jumpValue = 0;
        }
    }
    else if (jumpPressed)
    {
        rb.AddForce(new Vector3(0, Math.Abs(jumpValue) * jumpForce * 1, 0) , ForceMode.VelocityChange);//Absolute value to prevent weird cases of negative jAxis
        //jump key not pressed
        jumpPressed = false;
        jumpValue = 0;
    }
    else
    {
        jumpPressed = false;
    }

}
ifjumpPressed块与if jAxis>0块互斥是没有意义的。如果按下跳线,请先拆下else

此外,还应将jumpValue初始化为0f,因为编译器无法自行识别jumpPressed只有在设置jumpValue时才为真

您不需要else块将jumpPressed设置为false,因为如果它在该块中,它已经是false了

总之,这可能看起来是这样的:

void JumpHandler()
{

    float jAxis = Input.GetAxis("Jump");
    float jumpValue = 0f;

    //if pressed
    if (jAxis > 0)
    {
        bool isGrounded = CheckGrounded();

        if (isGrounded)
        {
            jumpValue = jAxis; 
            if (!jumpPressed)
            {
                jumpPressed = true; //Pressed jump
            }
        }
        else
        {
            jumpValue = 0;
        }
    }

    if (jumpPressed)
    {
        rb.AddForce(
                //Absolute value to prevent weird cases of negative jAxis
                new Vector3(0, Mathf.Abs(jumpValue) * jumpForce, 0),
                ForceMode.VelocityChange);
        //jump key not pressed
        jumpValue = 0;
        jumpPressed = false;
    }
}
这将使用checkground的伪方法生成如下输出,该方法返回true一次:

我不知道从哪里调用JumpHandler,但是使用bool标志等的整个计算对我来说没有什么意义

无需同时将值存储在局部变量jumpValue中。您只想在一个位置使用它,因此只需直接传入相应的值jAxis

因为您之前检查了jAxis>0,所以Mathf.Abs是多余的


当jAxis存在大量类似问题时,您将推迟到下次再次调用jumpHandler时实际执行跳转-请搜索错误消息并选择副本,以解释您的确切情况。我在开始时没有执行float jumpValue=0,因为这将始终使我的jumpValue在需要时为0向量3赋值。jumpPressed在程序的这一部分运行时也以false开头。每次调用JumpHandler时,jumpValue(因为它是一个局部变量)都会被取消设置,直到在方法运行时设置为止。对于rb.AddForce来说,这是可能的,并且在本例中是唯一可能的。。。在运行JumpHandler时未设置jumpValue的情况下运行的行。您是对的,我理解它为什么不运行,但我不知道如何在代码中绕过它。我能想到的唯一方法是在函数范围之外声明jumpValue,但我真的希望有另一种方法来声明它,而不重置其上的值。我所指的Vector3赋值是这样的:new Vector30,Math.AbsjumpValue*jumpForce*1,0以这种方式进行计算时,jumpValue始终为0。此外,文章的代码也被删除了,这就是为什么会有奇怪的格式。@LoganMcPhillips对我有用,请参阅答案中添加的gif。
void JumpHandler()
{
    float jAxis = Input.GetAxis("Jump");
    if(jAxis > 0 && CheckGrounded)
    {
        rb.AddForce(Vector3.up * jumpForce, ForceMode.VelocityChange);
    }
}