Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.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# 函数应返回true时返回false_C#_Unity3d - Fatal编程技术网

C# 函数应返回true时返回false

C# 函数应返回true时返回false,c#,unity3d,C#,Unity3d,我试图在Unity中创建一个基本形状,该形状在键盘输入的特定约束范围内旋转。然而,当我试图从最左边向右转弯时。似乎输入没有被调用是因为函数没有返回true,而据我所知,它应该返回true 有人知道为什么这不是真的吗?以下是脚本的其余部分(如果需要): using System.Collections; using System.Collections.Generic; using UnityEngine; public class ControlScript : MonoBehaviour

我试图在Unity中创建一个基本形状,该形状在键盘输入的特定约束范围内旋转。然而,当我试图从最左边向右转弯时。似乎输入没有被调用是因为函数没有返回true,而据我所知,它应该返回true

有人知道为什么这不是真的吗?以下是脚本的其余部分(如果需要):

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

public class ControlScript : MonoBehaviour {
public Rigidbody shipRig;
public int rotationSpeed = 1;
public int retRotationSpeed = 1;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
    Rigidbody tempShipRig = GetComponent<Rigidbody>();
    Vector3 shipEuler = shipRig.rotation.eulerAngles;

    //up input
    if (Input.GetKey(KeyCode.W) && CheckBounds(shipEuler.x, 0, 25) || 
Input.GetKey(KeyCode.W) && CheckBounds(shipEuler.x, 335, 360))
    {
        shipEuler.x = shipEuler.x + (-rotationSpeed * Time.deltaTime);
    }
    //down input
    else if (Input.GetKey(KeyCode.S) && CheckBounds(shipEuler.x, 0, 25) || 
Input.GetKey(KeyCode.S) && CheckBounds(shipEuler.x, 335, 360))
    {
        shipEuler.x = shipEuler.x + (rotationSpeed * Time.deltaTime);
    }
    else if(shipEuler.x !=0) //idle return to center
    {
        if(shipEuler.x < 180)
        {
            shipEuler.x = shipEuler.x - (retRotationSpeed * Time.deltaTime);
            if(shipEuler.x < 0)
            {
                shipEuler.x = 0;
            }
        }
        if (shipEuler.x > 180)
        {
            shipEuler.x = shipEuler.x + (retRotationSpeed * Time.deltaTime);
            if (shipEuler.x > 360)
            {
                shipEuler.x = 0;
            }
        }
    }
    //left input
    if (Input.GetKey(KeyCode.A) && CheckBounds(shipEuler.z, 0, 45) || 
Input.GetKey(KeyCode.A) && CheckBounds(shipEuler.z, 315, 360))
    {
        shipEuler.z = shipEuler.z + (rotationSpeed * Time.deltaTime);
    }
    //right input
    else if (Input.GetKey(KeyCode.D) && CheckBounds(shipEuler.z, 0, 45) || 
Input.GetKey(KeyCode.D) && CheckBounds(shipEuler.z, 315, 360))
    {
        shipEuler.z = shipEuler.z + (-rotationSpeed * Time.deltaTime);
    }


    if (shipEuler.x > 25 && shipEuler.x < 335) //fixes the rotation getting stuck when they pass over constraints
    {
        if(shipEuler.x < 180)
        {
            shipEuler.x = 25;
        }
        if(shipEuler.x > 180)
        {
            shipEuler.x = 335;
        }
    }
    if(shipEuler.z > 45 && shipEuler.z < 315)
    {
        if(shipEuler.z < 180)
        {
            shipEuler.z = 45;
        }
        if(shipEuler.z > 180)
        {
            shipEuler.z = 315;
        }
    }

    shipRig.rotation = Quaternion.Euler(shipEuler);
    shipRig = tempShipRig;

    Debug.Log(shipEuler);
}

bool CheckBounds (float orNr, float min, float max) //returns true if numbers are within constraints
{
    return (orNr <= max && orNr >= min);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类控制脚本:MonoBehavior{
公共刚体钻机;
公共整数旋转速度=1;
公共int追溯速度=1;
//用于初始化
无效开始(){
}
//每帧调用一次更新
无效更新(){
刚体tempShipRig=GetComponent();
Vector3 shipEuler=shipRig.rotation.eulerAngles;
//向上输入
if(Input.GetKey(KeyCode.W)和&CheckBounds(shipEuler.x,0,25)|
Input.GetKey(KeyCode.W)和CheckBounds(shipEuler.x335360))
{
shipEuler.x=shipEuler.x+(-旋转速度*时间增量);
}
//向下输入
else if(Input.GetKey(KeyCode.S)和CheckBounds(shipEuler.x,0,25)|
Input.GetKey(KeyCode.S)和CheckBounds(shipEuler.x,335360))
{
shipEuler.x=shipEuler.x+(旋转速度*时间增量);
}
否则如果(shipEuler.x!=0)//空闲返回中心
{
如果(Shippeler.x<180)
{
shipEuler.x=shipEuler.x-(追溯速度*时间.deltaTime);
如果(Shippeler.x<0)
{
x=0;
}
}
如果(Shippeler.x>180)
{
shipEuler.x=shipEuler.x+(追溯速度*时间.deltaTime);
如果(Shippeler.x>360)
{
x=0;
}
}
}
//左输入
if(Input.GetKey(KeyCode.A)和&CheckBounds(shipEuler.z,0,45)|
Input.GetKey(KeyCode.A)和CheckBounds(shipEuler.z,315360))
{
shipEuler.z=shipEuler.z+(旋转速度*时间增量时间);
}
//正确输入
else if(Input.GetKey(KeyCode.D)和CheckBounds(shipEuler.z,0,45)|
Input.GetKey(KeyCode.D)和CheckBounds(shipEuler.z,315360))
{
shipEuler.z=shipEuler.z+(-旋转速度*时间增量);
}
if(shipEuler.x>25&&shipEuler.x<335)//修复了通过约束时卡住的旋转
{
如果(Shippeler.x<180)
{
x=25;
}
如果(Shippeler.x>180)
{
shipEuler.x=335;
}
}
如果(shipEuler.z>45&&shipEuler.z<315)
{
如果(z<180)
{
z=45;
}
如果(z>180)
{
shipEuler.z=315;
}
}
shipRig.rotation=四元数.Euler(shipEuler);
shipRig=临时shipRig;
Log(shipEuler);
}
bool CheckBounds(float-orNr、float-min、float-max)//如果数字在约束范围内,则返回true
{
返回(orNr=min);
}
}

学习如何使用调试器的好时机..orNr是45,max是45,min呢?如果不看到矢量中填充
x
y
z
的代码,很难进行故障排除。您确定它们是介于0和360之间的值吗?例如,他们可能被设置为-180到180,或者其他什么。我没有看到任何处理完全旋转的代码,例如,在旋转后应用模。min将是0,如
if(Input.GetKey(KeyCode.A)和CheckBounds(shipeular.z,0,45)| Input.GetKey(KeyCode.A)和CheckBounds(shipeular.z,315,360))
shipprig.rotation.euleragles是(0.0,0,45.0)与您的问题无关,但有一个建议:代替if(shipEuler.x<0)shipEuler.x=0;如果(shipEuler.x<0)shipEuler.x+=360;如果(shipEuler.x>360)shipEuler.x=0,则相同;如果(shipEuler.x>360)shipEuler.x-=360,则应为;这样就不会在循环中丢失信息