-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConeCollider.cs
180 lines (158 loc) · 6.38 KB
/
ConeCollider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
using System.Collections;
using System.Collections.Generic;
public class ConeCollider : MonoBehaviour
{
[SerializeField, Range(0.01f, 88.5f)]
private float m_angle = 45;
[SerializeField]
private float m_distance = 1;
[SerializeField]
private bool m_isTrigger;
private Mesh m_mesh;
private Vector3 m_localScale;
[SerializeField]
private bool m_isFixScale = true;
void Awake()
{
//リソースロード
GameObject cone = Resources.Load("Prefab/ConeCollider") as GameObject;
//回転を初期位置に
var initRot = this.transform.rotation;
this.transform.rotation = Quaternion.LookRotation(Vector3.forward, Vector3.up);
//メッシュ情報作成
var coneMesh = cone.GetComponent<MeshFilter>().sharedMesh;
var vertices = coneMesh.vertices;
var triangles = coneMesh.triangles;
var forward = this.transform.TransformDirection(Vector3.forward);
var centerForwardPos = this.transform.position + forward * m_distance;
var harf = m_distance * Mathf.Tan(m_angle * Mathf.PI / 180f);
var verticleCount = 0;
//コーン円状部分のみ頂点座標移動
for (int i = 0; i < vertices.Length; i++)
{
var verticeWorldPos = vertices[i] + this.transform.position;
if (verticleCount != 2 || i >= 36)
{
//既に距離1分あるので-1
verticeWorldPos += forward * (m_distance - 1);
var outVec = (verticeWorldPos - centerForwardPos).normalized;
var outPos = centerForwardPos + outVec * harf;
//足した分を引く
vertices[i] = outPos - this.transform.position;
verticleCount++;
}
else
{
verticleCount = 0;
}
}
//新規メッシュ作成
m_mesh = new Mesh();
m_mesh.Clear();
m_mesh.vertices = vertices;
m_mesh.triangles = triangles;
MeshCollider meshCollider = this.gameObject.AddComponent<MeshCollider>();
meshCollider.sharedMesh = m_mesh;
meshCollider.convex = true;
meshCollider.isTrigger = m_isTrigger;
meshCollider.sharedMesh.RecalculateBounds();
meshCollider.sharedMesh.RecalculateNormals();
meshCollider.hideFlags = HideFlags.HideInInspector;
this.transform.rotation = initRot;
//ローカルスケール調整
if (m_isFixScale)
{
var scale = Vector3.one;
var parent = this.transform.parent;
while (true)
{
if (parent != null)
{
scale.x *= parent.localScale.x;
scale.y *= parent.localScale.y;
scale.z *= parent.localScale.z;
parent = parent.transform.parent;
}
else
{
break;
}
}
scale.x = 1.0f / scale.x;
scale.y = 1.0f / scale.y;
scale.z = 1.0f / scale.z;
this.transform.localScale = scale;
}
}
private void Start()
{
}
private void Update()
{
}
GameObject DebugObject(Vector3 pos, float scale = 1.0f, string name = "Sphere")
{
//デバッグ
var obj = GameObject.CreatePrimitive(PrimitiveType.Sphere);
obj.transform.position = pos;
obj.transform.localScale = new Vector3(scale, scale, scale);
obj.name = name;
return obj;
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(ConeCollider))]
[CanEditMultipleObjects]
public class ConeColliderEditor : Editor {
private SerializedProperty m_angle;
private SerializedProperty m_distance;
private SerializedProperty m_isTrigger;
private SerializedProperty m_isFixScale;
private ConeCollider m_conecollider;
void OnEnable() {
SetProperty(ref m_angle, "m_angle");
SetProperty(ref m_distance, "m_distance");
SetProperty(ref m_isTrigger, "m_isTrigger");
SetProperty(ref m_isFixScale, "m_isFixScale");
m_conecollider = target as ConeCollider;
}
public override void OnInspectorGUI() {
serializedObject.Update();
{
DrawPropertyField(m_angle, "Angle");
DrawPropertyField(m_distance, "Distance");
DrawPropertyField(m_isTrigger, "isTrigger");
DrawPropertyField(m_isFixScale, "isFixScale");
}
serializedObject.ApplyModifiedProperties();
}
void OnSceneGUI() {
if (!EditorApplication.isPlaying) {
m_distance.floatValue = m_distance.floatValue < 1.0f ? 1.0f : m_distance.floatValue;
var centerForward = m_conecollider.transform.position + m_conecollider.transform.TransformDirection(Vector3.forward) * m_distance.floatValue;
var harf = m_distance.floatValue * Mathf.Tan(m_angle.floatValue * Mathf.PI / 180f);
var up = centerForward + m_conecollider.transform.TransformDirection(Vector3.up) * harf;
var down = centerForward + m_conecollider.transform.TransformDirection(Vector3.down) * harf;
var right = centerForward + m_conecollider.transform.TransformDirection(Vector3.right) * harf;
var left = centerForward + m_conecollider.transform.TransformDirection(Vector3.left) * harf;
Handles.color = new Color(0.53f, 0.82f, 0.5f);
Handles.DrawLine(m_conecollider.transform.position, up);
Handles.DrawLine(m_conecollider.transform.position, down);
Handles.DrawLine(m_conecollider.transform.position, right);
Handles.DrawLine(m_conecollider.transform.position, left);
Handles.CircleHandleCap(0, centerForward, m_conecollider.transform.rotation, harf, EventType.Repaint);
Handles.color = Color.white;
}
}
void SetProperty(ref SerializedProperty property, string name) {
property = serializedObject.FindProperty(name);
}
void DrawPropertyField(SerializedProperty property, string name) {
EditorGUILayout.PropertyField(property, new GUIContent(name));
}
}
#endif