범위

2017-11-30 2 views
1
을 유지하면서 시야를 증가하는 방법

그래서 3 벡터 점하게 생성 할 수있는 다음과 같은 기능을 가지고 삼각형을 만듭니다범위

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

오전 데 문제는 그 내가 각도를 증가의 울렸다 감소하는 것은 나에게 어떤 의미를 가진다. 예를 들어

, 45 각도 :

enter image description here

135 각도 :

enter image description here

내가 범위를 유지하기 위해 계산을 수정하는 방법입니다 도움이 필요한 문제 라인 (다른 녹색 직선)은 각도에 관계없이 일정합니다 (170도에서 제한됩니다).

각도와 함께 움직이는 범위를 얻을 수는 있지만 임의의 계산을 수행 할 수는 있지만 일관성이 없으며 (적어도 내가 시도한 임의의 숫자로는) 작동하지 않습니다. 나는 또한 hypotenuse 공식에 대해서 생각했지만 단 한 변의 길이 (범위)를 알고있다.

도움이 될만한 내용이 있으면 도움이 될 것입니다. 여기

+1

[삼각 함수] (https : //로 i.stack. imgur.com/OEl9Y.png). x를 구하십시오. – Draco18s

+0

Draco가 제안한 것처럼 삼각형을 두 개로 나누면 "[The Law of Sines] (https://en.wikipedia.org/wiki/Law_of_sines)"를 사용하여 문제를 해결할 수 있습니다. [수학] (https://math.stackexchange.com)에서 전체 답을 얻을 수 있습니다. 주된 문제는 랑 그드에 상관없는 삼각법 문제입니다. – Foggzie

답변

2

:

스크립트 :

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

} 

출력 :이 도움이 :)

희망

+0

이봐 요, 이건 매우 도움이 되었어요. 왼쪽/오른쪽 위치에'+ transform.position'을 추가해야만했습니다. https://stackoverflow.com/a/13695630/384016과 함께이 스크립트는 저의 기본 뷰 필드 스크립트를 설치하고 실행합니다. – ryanzec