C# Unity3D摄像机不断移动

C# Unity3D摄像机不断移动,c#,camera,unity3d,C#,Camera,Unity3d,我正试图在unity中制作我自己的第一人称相机控件,它在一个操纵台上使用两个拇指。我的这个很好用。然而,当我移动右拇指(用来向左/向右和向上/向下看的那根)时,相机开始不断更新 这是我的操纵手柄移动代码: // Update is called once per frame void Update() { float speed = transform.localEulerAngles.y + Input.GetAxis("LeftRight") * sensitivityX;

我正试图在unity中制作我自己的第一人称相机控件,它在一个操纵台上使用两个拇指。我的这个很好用。然而,当我移动右拇指(用来向左/向右和向上/向下看的那根)时,相机开始不断更新

这是我的操纵手柄移动代码:

  // Update is called once per frame
void Update()
{

    float speed = transform.localEulerAngles.y + Input.GetAxis("LeftRight") * sensitivityX;

    rotationY += Input.GetAxis("UpDown") * sensitivityY;
    rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);

    transform.localEulerAngles = new Vector3(-rotationY, speed, 0);

}
为了控制左手拇指,我只需将我的脚本附加到第一人称角色控制器上

我知道我的代码正在更新每一帧,但我假设它只会在我按下右拇指时更新。如果不是这样,有没有办法限制它在我移动右拇指时只增加?

update()中的代码确实会在每一帧执行

您注意到的移动主要是由于每帧调用
transform.localeuleranges
。 speed参数从不为零,因为它基于
transform.localEulerAngles.y

您可以检查轴值是否不为零,如下所示:

void Update()
{

    float inputValueLeftRight = Input.GetAxis("LeftRight");
    if(inputValueLeftRight > 0 || inputValueLeftRight < 0)
    {
       float speed = transform.localEulerAngles.y + inputValueLeftRight * sensitivityX;    
       transform.localEulerAngles = new Vector3(-rotationY, speed, 0);
    }

    float inputValueUpDown = Input.GetAxis("UpDown");
    if(inputValueUpDown > 0 || inputValueUpDown < 0)
    {
       rotationY += inputValueUpDown * sensitivityY;
       rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
    }

}
void Update()
{
float inputValueLeftRight=Input.GetAxis(“LeftRight”);
如果(inputValueLeftRight>0 | | inputValueLeftRight<0)
{
浮动速度=transform.localeurangles.y+输入值leftright*灵敏度x;
transform.localEulerAngles=新矢量3(-rotationY,speed,0);
}
float inputValueUpDown=Input.GetAxis(“UpDown”);
如果(inputValueUpDown>0 | | inputValueUpDown<0)
{
旋转Y+=输入值上下*灵敏度yy;
旋转Y=数学夹具(旋转Y、最小值、最大值);
}
}
Update()中的代码确实会在每一帧执行一次

您注意到的移动主要是由于每帧调用
transform.localeuleranges
。 speed参数从不为零,因为它基于
transform.localEulerAngles.y

您可以检查轴值是否不为零,如下所示:

void Update()
{

    float inputValueLeftRight = Input.GetAxis("LeftRight");
    if(inputValueLeftRight > 0 || inputValueLeftRight < 0)
    {
       float speed = transform.localEulerAngles.y + inputValueLeftRight * sensitivityX;    
       transform.localEulerAngles = new Vector3(-rotationY, speed, 0);
    }

    float inputValueUpDown = Input.GetAxis("UpDown");
    if(inputValueUpDown > 0 || inputValueUpDown < 0)
    {
       rotationY += inputValueUpDown * sensitivityY;
       rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
    }

}
void Update()
{
float inputValueLeftRight=Input.GetAxis(“LeftRight”);
如果(inputValueLeftRight>0 | | inputValueLeftRight<0)
{
浮动速度=transform.localeurangles.y+输入值leftright*灵敏度x;
transform.localEulerAngles=新矢量3(-rotationY,speed,0);
}
float inputValueUpDown=Input.GetAxis(“UpDown”);
如果(inputValueUpDown>0 | | inputValueUpDown<0)
{
旋转Y+=输入值上下*灵敏度yy;
旋转Y=数学夹具(旋转Y、最小值、最大值);
}
}

请记住,我不知道检查输入值上的精确零是否可行。您可能需要留下一点间隙,但我无法确定该间隙应该有多大。请尝试尝试一下。请记住,我不知道检查输入值上的精确零是否可行。您可能需要留下一点间隙有一点差距,但我无法确定这个差距应该有多大。尝试一下。