1

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.

What I have so far

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.

enter image description here enter image description here

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.

  • Sorry, I can't make out from your img which cone you're talking about? What is the meaning of `angleInDegrees` - is it the angle you want the cone to point in? Presumably you want the cone to rotate about the `X` axis, and your angles are measured in that orientation. Also I'm confused when you say "the cone faces the X-Axis"? This kind of thing is hard to explain in words, even a hand drawn diagram would help. – Jim W May 14 '19 at 17:10
  • The cone in the image is the one above the player's head, within the circle. Yes, angleInDegrees is the angle that controls where the cone is pointing. The edges of the white circle around my player stay to either side of him when he rotates, I want the same for the cone – Matthew Mcginley May 14 '19 at 17:35
  • https://www.dropbox.com/s/z9ybypxencl35os/p1.PNG?dl=0 https://www.dropbox.com/s/wr1ktnfz8cueywq/p2.PNG?dl=0 These two images show how the circle rotates with the player, but the cone does not – Matthew Mcginley May 14 '19 at 17:36
  • 1
    It's still unclear what the meaning of the returned value is meant to be. Please edit your post and include the code that uses the returned value from `DirFromAngle` to rotate/render the circle & field of view. – Ruzihm May 14 '19 at 17:57
  • Sorry I can't get my head around this - what is the circle for? Is the 'cone' in question the green one drawn by Unity to point in the y axis? Or do you mean the 2 lines that make the circle segment? You should use a paint tool to paint what you want to happen. – Jim W May 14 '19 at 17:59
  • To be clear, posting the outside code is important because depending on the code using the return value, it may not be able to render the field of view properly even if we were to try every possible `Vector3` output of `DirFromAngle` – Ruzihm May 14 '19 at 18:04

1 Answers1

0

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;
}    
Ruzihm
  • 19,749
  • 5
  • 36
  • 48
  • Unfortunately, it didn't work. It just extended the cone lines. But, I think you're on the right track. I need to do something with my player's rotation so that the cone knows where to rotate. Thanks for trying. – Matthew Mcginley May 14 '19 at 18:48
  • @MatthewMcginley My code won't/shouldn't change the length of `newValue` as written. Can you edit your post and include what you tried? I think there might be a typo. Make sure you're multiplying `newValue` by the `Quaternion` you get from the call to `Quaternion.Euler`. – Ruzihm May 14 '19 at 18:53
  • Sorry, I got it wrong. You are a life-saver. I have been stuck on this for three days. I really appreciate your help. Everything rotates as it should. Thank you so much – Matthew Mcginley May 14 '19 at 19:00
  • 1
    https://www.dropbox.com/s/5jxmkw480oroukv/yay.mp4?dl=0 Had to alter your just a tiny bit to get facing the right way. ```c# Quaternion yRotation = Quaternion.Euler(0f, yRotationAngle + 90, 0f); ``` Overall, everything works great. Thanks again. – Matthew Mcginley May 14 '19 at 19:04
  • @MatthewMcginley thanks! I'll include that in my answer. Glad I could help. – Ruzihm May 14 '19 at 19:07