Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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# Unity 3D-如何在Mouselook脚本中使用LateUpdate?_C#_Unity3d - Fatal编程技术网

C# Unity 3D-如何在Mouselook脚本中使用LateUpdate?

C# Unity 3D-如何在Mouselook脚本中使用LateUpdate?,c#,unity3d,C#,Unity3d,当我旋转物体时,我的身体会出现严重的口吃/延迟。 在本论坛中,我被建议将刚体设置为“插值”,并在脚本中使用LateUpdate 问题是…我不知道怎么做? 当我尝试时,我得到编译器错误或只是无法移动凸轮 我找到了另一个免费的FPS脚本,通过使用摄影机跟随延迟,但我也不知道如何使用该代码 如何在当前脚本中使用LateUpdate void Start() { Cursor.visible = false; Cursor.lockState = CursorLockMode.Locke

当我旋转物体时,我的身体会出现严重的口吃/延迟。 在本论坛中,我被建议将刚体设置为“插值”,并在脚本中使用LateUpdate

问题是…我不知道怎么做? 当我尝试时,我得到编译器错误或只是无法移动凸轮

我找到了另一个免费的FPS脚本,通过使用摄影机跟随延迟,但我也不知道如何使用该代码

如何在当前脚本中使用LateUpdate

void Start() {
    Cursor.visible = false;
    Cursor.lockState = CursorLockMode.Locked;
}

void Update() {
    float x = Input.GetAxis("Mouse X") * sensitivity * Time.deltaTime;
    float y = Input.GetAxis("Mouse Y") * sensitivity * Time.deltaTime * -1f;

    transform.Rotate(0f, x, 0f);

    headRotation += y;
    headRotation = Mathf.Clamp(headRotation, -headRotationLimit, headRotationLimit);
    cam.localEulerAngles = new Vector3(headRotation, 0f, 0f);
}

这是我的FPS播放器旋转方法的一个编辑版本,将此作为如何操作的指南,并根据您的需要对其进行修改,包括我用于限制相机旋转距离的两种方法(播放器可以上下查看)


我猜你指的是最新更新?是的,先生,但是当我将更新更改为最新更新时,相机仍然不旋转。(没有口吃)。我有500多帧。
Lerp
相机的旋转使其平滑,但您希望在
最新更新中执行此操作是正确的。
LateUpdate
。代码中的外观如何?您的脚本中有编译器错误。63,19“四元数不包含ClampRotationXAxis的定义,并且找不到接受四元数类型的第一个参数的可访问扩展方法ClampRotationXAxis”。很抱歉,我没有完全删除使用
ClampRotationXAxis()
作为扩展更改的方式
q.ClampRotationXAxis(minX,maxX)
claprotationxaxis(q,minX,maxX)谢谢您抽出时间。脚本现在运行,但我无法移动摄影机。我试着把相机和播放器连在一起。我可能遗漏了什么?这些方法将出现在您用于播放器上其他播放器控件的脚本中,您是否在
最新更新中添加了对旋转方法的调用?好的,我将尝试使用它。我的编码技能相当基本。非常感谢。
// Set the Camera in the inspector so that it can be rotated
public Camera camera;


// Rotation variables for both the player and camera
private Quaternion _playerRotation;
private Quaternion _cameraRotation;

// The speed you want to be able to look
private float _lookSpeed = 3f;

// Set the initial rotations to the rotation variables
private void Awake()
{
    _playerRotation = transform.localRotation;
    _cameraRotation = camera.transform.localRotation;
}

// Method to be called from LateUpdate
private void UpdateRotation()
{
    Vector2 input = new Vector2(
        Input.GetAxis("Mouse X"),
        Input.GetAxis("Mouse Y")
    );

    if (input != Vector2.zero)
    {
        _playerRotation *= Quaternion.Euler(0f, _lookSpeed * input.x, 0f);
        _cameraRotation *= Quaternion.Euler(_lookSpeed * -input.y, 0f, 0f);

        _cameraRotation = ClampRotationAroundXAxis(_cameraRotation, -50f , 75f);
    }

    transform.localRotation = Quaternion.Slerp(transform.localRotation, _playerRotation, 10f * Time.deltaTime);
    camera.transform.localRotation = Quaternion.Slerp(camera.transform.localRotation, _cameraRotation, 10f * Time.deltaTime);
}


// A couple of rotation methods 
// I suggest making these extensions for use anywhere in the game and for all rotations
private Quaternion ClampRotationAroundXAxis(Quaternion q, float minX = 0f, float maxX = 0f)
{
    if (q.w != 0f)
    {
        q.x /= q.w;
        q.y /= q.w;
        q.z /= q.w;
        q.w = 1f;
    }

    q.y = 0f;
    q.z = 0f;

    if (minX != 0f || maxX != 0f)
    {
        q = ClampRotationXAxis(q, minX, maxX);
    }

    return q;
}

private Quaternion ClampRotationXAxis(Quaternion q, float min, float max)
{

    if (q.w != 0f)
    {
        q.x /= q.w;
        q.y /= q.w;
        q.z /= q.w;
        q.w = 1f;
    }

    float angle = 2f * Mathf.Rad2Deg * Mathf.Atan(q.x);

    angle = Mathf.Clamp(angle, min, max);

    q.x = Mathf.Tan(0.5f * Mathf.Deg2Rad * angle);

    return q;
}