Vector3.Dot (내적)
Unity3D 2020. 11. 24. 17:25반응형
Vector3.Dot, 내적, Fov, 시야각
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Fov))]
public class FovEditor : Editor
{
private void OnSceneGUI()
{
Fov fov = (Fov)this.target;
//주어진 각도의 1/2
Vector3 fromAnglePos = fov.CirclePoint(-fov.angle * 0.5f);
fov.anglePos = fromAnglePos;
Handles.color = new Color(1, 1, 1, 0.3f);
Handles.DrawWireDisc(fov.gameObject.transform.position, //원점 좌표
Vector3.up, //노멀 벡터
fov.area); //원의 반지름
Handles.DrawSolidArc(fov.transform.position, //원점
Vector3.up, //노멀 벡터
fromAnglePos, //시작위치
fov.angle, //각도
fov.area); //반지름
GUIStyle guiStyle = new GUIStyle();
guiStyle.fontSize = 40;
Handles.Label(fov.transform.position + fov.transform.forward, fov.angle.ToString(), guiStyle);
//fov.area =
// Handles.ScaleValueHandle(fov.area,
// fov.transform.position + fov.transform.forward * fov.area,
// fov.transform.rotation,
// 1,
// Handles.ConeHandleCap,
// 1);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Fov : MonoBehaviour
{
public float area = 1f;
public float angle = 60f;
public Vector3 anglePos;
public Vector3 CirclePoint(float angle) {
angle += this.transform.eulerAngles.y;
return new Vector3(Mathf.Sin(angle * Mathf.Deg2Rad), 0, Mathf.Cos(angle * Mathf.Deg2Rad));
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
public class FovMain : MonoBehaviour
{
private List<Enemy> list;
public GameObject hero;
private Fov fov;
void Start()
{
this.fov = this.hero.GetComponent<Fov>();
this.list = FindObjectsOfType<Enemy>().ToList();
}
// Update is called once per frame
void Update()
{
foreach (var enemy in this.list) {
var toOther = enemy.transform.position - this.hero.transform.position;
var angle = Vector3.Angle(this.hero.transform.forward, toOther);
var distance = toOther.magnitude;
if (distance <= this.fov.area && angle < this.fov.angle / 2) {
Debug.DrawLine(this.hero.transform.position, enemy.gameObject.transform.position, Color.red);
}
}
}
}
반응형
'Unity3D' 카테고리의 다른 글
유도탄 (내적 + 외적) (0) | 2020.11.25 |
---|---|
unity skill indicator shader (0) | 2020.11.24 |
TransformDirection, InverseTransformDirection, TransformPoint, InverseTransformPoint (0) | 2020.11.24 |
Cinemachine Camera (0) | 2020.11.19 |
Vector3.Project (4) | 2020.10.27 |