简体   繁体   English

播放器旋转视野

[英]Rotating field of view with player

So I have trying to create a system that looks for a ledge to climb. 因此,我试图创建一个系统来寻找要爬的壁架。 I have a vertical field of view that looks for these ledges and I have, so far, got it to rotate with the analogue stick's direction, so I can find a ledge and move to it where my analogue is pointing. 我有一个垂直的视野可以寻找这些壁架,到目前为止,我已经使其沿模拟摇杆的方向旋转,因此我可以找到一个壁架并移动到模拟物指向的位置。 (Look up, find ledge, move up, etc). (查找,查找壁架,向上移动等)。 My problem is, while the the field of view moves where I need it to move, it stays fixed on one axis and does not rotate with the player. 我的问题是,当视野移动到需要移动的位置时,它保持固定在一个轴上,并且不会随播放器旋转。

到目前为止我有什么

In the picture the cone is facing where it should be, and rotates around the player as I want it to, but as you can see, the cone faces the X-Axis and does not move from there. 在图片中,圆锥体朝向应该放置的位置,并按照我想要的方向围绕播放器旋转,但是如您所见,圆锥体面向X轴并且不会从那里移动。 I want it to rotate with the player. 我希望它随播放器一起旋转。

In the below code, the returned Vector's y value makes the cone point upwards. 在下面的代码中,返回的Vector的y值使圆锥点向上。 I have tried all manner of functions in the x and z values, but nothing is sticking. 我已经尝试了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;
}

The viewDir value makes the cone rotate with my analogue stick, to make the cone rotate with my player, I assume that something needs to be done with either the x or z values, or both, but for the life of me, I can't figure it out. viewDir值使圆锥体随着我的模拟棒旋转,使圆锥体随我的播放器旋转,我假设需要使用x或z值或同时使用这两个值来做某件事,但是对于我来说,我可以t弄清楚。 Thank you for any help on this issue. 感谢您在此问题上的任何帮助。 Apologies if my explanation isn't very good. 抱歉,如果我的解释不是很好。

在此处输入图片说明 在此处输入图片说明

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);
    }

This is the editor script that displays the field of view. 这是显示视野的编辑器脚本。

The circle simply represents the radius of the field of view, how far it goes out. 圆仅表示视场的半径,其走多远。

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

In the video, the circle rotates with the player, and the cone rotates with my left analogue movements. 在视频中,圆圈随播放器旋转,圆锥随我的左模拟动作旋转。 I want the cone to be like the circle, with it rotating as my player does, but still matching my analogue movements. 我希望圆锥像圆一样,随着玩家的旋转而旋转,但仍与模拟运动匹配。

One way to approach this is to find the Y rotation of the player and apply it to newValue before returning it. 一种解决方法是找到播放器的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;
}    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM