简体   繁体   中英

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. I want it to rotate with the player.

In the below code, the returned Vector's y value makes the cone point upwards. I have tried all manner of functions in the x and z values, but nothing is sticking.

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. 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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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