1

So I have the following function to generate 3 vector points let creates a triangle:

private Vector3[] GetFieldOfViewPoint() {
  float Range = 3f;
  float Angle = 45;

  Vector3 offset = new Vector3(0, Range, 0);
  Quaternion rotation1 = Quaternion.Euler(0, 0, -Angle / 2);
  Quaternion rotation2 = Quaternion.Euler(0, 0, Angle / 2);

  return new Vector3[3] {
    transform.position,
    (rotation1 * offset) + transform.position,
    (rotation2 * offset) + transform.position
  };
}

The issue I am having is that when I increase the angle, the rang decrease which does make some sense to me.

For example, 45 angle:

enter image description here

135 angle:

enter image description here

The issue I need help with is how can I modify the calculation in order to keep the range line (the furtherest straight green line) consistent regardless of the angle (which will be capped at 170 degrees)?

I can get the range to move with the angle but doing random calculations but it is not consistent and just does not work (at least with the random number I have tried). I also thought about hypotenuse formula however I only know 1 sides length (which is the range).

Any help with this would be awesome.

ryanzec
  • 27,284
  • 38
  • 112
  • 169
  • 1
    [Trigonometry](https://i.stack.imgur.com/OEl9Y.png). Solve for x. – Draco18s no longer trusts SE Nov 30 '17 at 02:02
  • If you split that triangle in two like Draco suggests, you can use "[The Law of Sines](https://en.wikipedia.org/wiki/Law_of_sines)" to solve it. You'd get a full answer over at [Mathematics](https://math.stackexchange.com); the main question is a langue-agnostic trigonometry problem. – Foggzie Nov 30 '17 at 05:03

1 Answers1

2

Here:

Script:

using UnityEngine;

public class TrignometryTest : MonoBehaviour 
{
    public float range;
    [Range(5,170)]
    public float angle;

    Vector3 size = Vector3.one;

    Vector3[] GetFOVPoints(float _range, float _angleInDegrees)
    {
        Vector3 rightPoint, leftPoint;
        float halfAngle = _angleInDegrees/2;

        rightPoint = new Vector3(_range * Mathf.Tan(halfAngle * Mathf.Deg2Rad), _range, 0);
        leftPoint = new Vector3(-_range * Mathf.Tan(halfAngle * Mathf.Deg2Rad), _range, 0);

        Vector3[] points =  { transform.position, leftPoint, rightPoint};
        return points;
    }

    void OnDrawGizmos()
    {
        var points = GetFOVPoints(range, angle);

        Gizmos.DrawCube(points[0], size);
        Gizmos.DrawCube(points[1], size);
        Gizmos.DrawCube(points[2],size);

        Gizmos.DrawLine(points[0], points[1]);
        Gizmos.DrawLine(points[0], points[2]);
        Gizmos.DrawLine(points[1], points[2]);
        Gizmos.DrawLine(points[0],Vector3.up * range);
    }

}

OutPut:

Hope this helps :)

Umair M
  • 10,298
  • 6
  • 42
  • 74
  • Hey, this was very helpful, only thing I needed to do what add `+ transform.position` to the left / right position. This along with https://stackoverflow.com/a/13695630/384016 has gotten me my basic field of view script up and running. – ryanzec Dec 01 '17 at 00:05