与玩家一起旋转视野

Rotating field of view with player

所以我试图创建一个系统来寻找要攀登的壁架。我有一个垂直视野来寻找这些壁架,到目前为止,我已经让它随着模拟摇杆的方向旋转,所以我可以找到一个壁架并移动到我的模拟装置指向的地方。 (向上看,找到壁架,向上移动等)。我的问题是,当视野移动到我需要它移动的地方时,它保持固定在一个轴上并且不随玩家旋转。

在图片中,锥体朝向它应该在的位置,并按照我的意愿围绕玩家旋转,但如您所见,锥体朝向 X-Axis 并且不会从那里移动。我想让它和播放器一起旋转。

在下面的代码中,返回的Vector的y值使圆锥向上。我在 x 和 z 值中尝试了各种函数,但没有任何问题。

public Vector3 DirFromAngle(float angleInDegrees, bool angleIsGlobal)
{
    if(!angleIsGlobal)
    {
        angleInDegrees += (viewDir + 90);
    }

    Vector3 newValue = new Vector3(0, Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));
    return newValue;
}

viewDir 值使圆锥体随着我的模拟摇杆旋转,为了使圆锥体随着我的播放器旋转,我假设需要用 x 或 z 值或两者来做一些事情,但为了生命我,我想不通。 感谢您在此问题上提供的任何帮助。如果我的解释不是很好,请见谅。

void OnSceneGUI()
    {
        LedgePointSearch lps = (LedgePointSearch)target;
        Handles.color = Color.white;
        Handles.DrawWireArc(lps.transform.position, lps.transform.forward * 90, Vector3.up, 360, lps.viewRadius);
        Vector3 viewAngleA = lps.DirFromAngle(-lps.viewAngle / 2, false);
        Vector3 viewAngleB = lps.DirFromAngle(lps.viewAngle / 2, false);

        Handles.DrawLine(lps.transform.position, lps.transform.position + viewAngleA * lps.viewRadius);
        Handles.DrawLine(lps.transform.position, lps.transform.position + viewAngleB * lps.viewRadius);
    }

这是显示视野的编辑器脚本。

圆圈简单的代表视野的半径,出去多远。

https://www.dropbox.com/s/k9siha2aog9vj8o/fov.mp4?dl=0

在视频中,圆圈随着播放器旋转,圆锥体随着我左手的模拟动作旋转。我希望圆锥体像圆一样,像我的播放器一样旋转,但仍与我的模拟动作相匹配。

解决此问题的一种方法是找到玩家的 Y 轴旋转并将其应用于 newValue,然后再返回它。

public Vector3 DirFromAngle(float angleInDegrees, bool angleIsGlobal)
{
    if(!angleIsGlobal)
    {
        angleInDegrees += (viewDir + 90);
    }

    Vector3 newValue = new Vector3(0, Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), Mathf.Cos(angleInDegrees * Mathf.Deg2Rad));

    // get Y rotation of player
    yRotationAngle = player.transform.rotation.eulerAngles.y;

    // convert to Quaternion using only the Y rotation
    Quaternion yRotation = Quaternion.Euler(0f, yRotationAngle + 90f, 0f);

    // Apply yRotation
    newValue = yRotation * newValue;

    return newValue;
}