C# 统一头Y轴旋转夹具

C# 统一头Y轴旋转夹具,c#,unity3d,C#,Unity3d,我试着在unity中创建一个FPP模式,在那里你可以看到你的真实身体。我创造了一个模型,操纵了一切。我的头会对着摄像机旋转,但我不希望球员能够绕着他的身体旋转。我已经在x轴上夹紧旋转,但在y轴上夹紧时有问题 void Update () { currentBodyRotation = body.GetComponent<Transform> ().rotation.eulerAngles.y; yaw += Input.GetAxis ("Mouse X") * mouse

我试着在unity中创建一个FPP模式,在那里你可以看到你的真实身体。我创造了一个模型,操纵了一切。我的头会对着摄像机旋转,但我不希望球员能够绕着他的身体旋转。我已经在x轴上夹紧旋转,但在y轴上夹紧时有问题

void Update () {
currentBodyRotation = body.GetComponent<Transform> ().rotation.eulerAngles.y;

    yaw += Input.GetAxis ("Mouse X") * mouseSensitivity;


    yawMin = currentBodyRotation - 90f;
    yawMax = currentBodyRotation + 90f;

    yaw =  Mathf.Clamp (yaw, yawMin, yawMax);



    pitch -= Input.GetAxis ("Mouse Y") * mouseSensitivity;
    pitch = Mathf.Clamp (pitch, pitchMinMax.x, pitchMinMax.y);


    currentRotation = Vector3.SmoothDamp (currentRotation, new Vector3 (pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
    transform.rotation = Quaternion.Euler (currentRotation);

}
void更新(){
currentBodyRotation=body.GetComponent().rotation.eulerAngles.y;
yaw+=Input.GetAxis(“鼠标X”)*鼠标灵敏度;
yawMin=当前车身旋转-90f;
yawMax=当前车身旋转+90f;
偏航=主夹钳(偏航、偏航最小、偏航最大);
pitch-=Input.GetAxis(“鼠标Y”)*鼠标灵敏度;
节距=数学夹具(节距、节距最小值x、节距最小值y);
currentRotation=Vector3.SmoothDamp(currentRotation,新矢量3(俯仰、偏航)、参考旋转平滑速度、旋转平滑时间);
transform.rotation=四元数.Euler(currentRotation);
}

我认为旋转在0和360度角时有限制。我的代码可以完美工作,直到身体达到360度。当它这样做的时候,我的相机会猛地一跳,然后从看不见的墙上“反弹”到它原来的一面。

好的。我已经弄明白了。这里的代码,如果有人有类似的问题,也许它也会为他们工作

void Update () {

    yaw += Input.GetAxis ("Mouse Y") * mouseHorizontalSensitivity;


    yaw =  Mathf.Clamp (yaw, -30f, 80f);  // It's pitch but my code works weird



    pitch -= Input.GetAxis ("Mouse X") * mouseVerticalSensitivity;
    pitch = Mathf.Clamp (pitch, -90f, 90f);  // it's yaw


    currentRotation = Vector3.SmoothDamp (currentRotation, new Vector3 (-pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
    transform.localEulerAngles = currentRotation;

// my mouse input wouldn't go down which caused the model to rotate indeffinitely so i added smoothing to 0 for rotating around Y axis which here is labeled as pitch
    if (!Input.GetKey (KeyCode.LeftAlt))
        pitch = Mathf.SmoothDamp(pitch, 0f, ref rotationSmoothVelocity2, rotationSmoothTime); 

}

尝试设置摄像头限制。这应该行得通,但行不通。transform.localEulerAngles搞乱了我的相机和动画。编辑:我已经修好了,但仍然不起作用。它停止了我的部分鼠标输入。我以为你移动相机时有一些角度限制。该代码适用于FPS相机,应该附加到没有动画的相机上。现在几乎所有东西都可以工作了,但我仍然需要弄清楚如何将相机旋转到正确的位置。每次我启动测试时,我的相机都会绕Z轴旋转90度。