回答編集履歴
2
コメントに対して追記
test
CHANGED
@@ -5,3 +5,135 @@
|
|
5
5
|
衝突相手の情報を得るのでしたら、[Physics.CheckSphere](https://docs.unity3d.com/ja/current/ScriptReference/Physics.CheckSphere.html)の代わりに[Physics.OverlapSphere](https://docs.unity3d.com/ja/current/ScriptReference/Physics.OverlapSphere.html)、または[Physics.OverlapSphereNonAlloc](https://docs.unity3d.com/ja/current/ScriptReference/Physics.OverlapSphereNonAlloc.html)を使ってみてはいかがでしょう。
|
6
6
|
|
7
7
|
ただ、球領域と接触するコライダーは得られるものの、[OnCollisionEnter](https://docs.unity3d.com/ja/current/ScriptReference/MonoBehaviour.OnCollisionEnter.html)のような衝突に関する詳しい情報は得られないのは難点かもしれませんね。どちらかというと[OnTriggerEnter](https://docs.unity3d.com/ja/current/ScriptReference/MonoBehaviour.OnTriggerEnter.html)に近い動作になるかと思います。
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
#コメントについて追記
|
12
|
+
|
13
|
+
**1点目**
|
14
|
+
|
15
|
+
空のオブジェクトでも描画されるはずですが、なぜでしょうかね...?
|
16
|
+
|
17
|
+
可能性としては、この`Sample`スクリプトでは、ギズモ描画位置の決定にフィールド`center`を使っています。この`center`にオブジェクトの座標がセットされるのは`Start`メソッドが実行された後ですので、シーン編集中の段階では`center`に座標がセットされず`(0, 0, 0)`のままとなり、ワールド原点に球が描画されてしまっている...ということがありそうな気がします。ワールド原点がシーンビューの画面外に出てしまっている場合、一見何も描画されなかったように見えてしまうかもしれません。
|
18
|
+
|
19
|
+
ワールド原点に球が描画されているか確認してみたり、あるいはプレイボタンを押して実行してみてください。プレイモードにすると`center`にオブジェクトの座標がセットされ、オブジェクトの位置に球が現れるかと思います。
|
20
|
+
|
21
|
+
もし、プレイモードにしてもやっぱりオブジェクトの位置に球が現れないようなら、なにか他の原因でしょうね...その場合はコメントいただければ、さらに調査してみようと思います。
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
**2点目**
|
28
|
+
|
29
|
+
1点目とも関連している話ですが、`center`がセットされるのは`Start`実行時の一回きりです。ですので実行中にオブジェクトを動かそうが、球の位置は`Start`時の位置から変わらないはずです。
|
30
|
+
|
31
|
+
もし常にオブジェクトの座標に追従させたいのでしたら、そもそも`center`を覚えておくのはやめて、常に最新の位置を球の中心にしてしまってはいかがでしょうか?
|
32
|
+
|
33
|
+
```C#
|
34
|
+
|
35
|
+
using System.Collections;
|
36
|
+
|
37
|
+
using System.Collections.Generic;
|
38
|
+
|
39
|
+
using UnityEngine;
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
public class Sample : MonoBehaviour
|
44
|
+
|
45
|
+
{
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
[SerializeField]
|
50
|
+
|
51
|
+
float sphereRadius = 3.0f;
|
52
|
+
|
53
|
+
// Vector3 center; // centerは廃止
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
// Startも不要なので削除
|
58
|
+
|
59
|
+
/*
|
60
|
+
|
61
|
+
// Use this for initialization
|
62
|
+
|
63
|
+
void Start()
|
64
|
+
|
65
|
+
{
|
66
|
+
|
67
|
+
center = this.transform.position;
|
68
|
+
|
69
|
+
}
|
70
|
+
|
71
|
+
*/
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
// Update is called once per frame
|
76
|
+
|
77
|
+
void Update()
|
78
|
+
|
79
|
+
{
|
80
|
+
|
81
|
+
Collider[] hitColliders = Physics.OverlapSphere(transform.position, sphereRadius); // 常にtransform.positionを見るよう変更
|
82
|
+
|
83
|
+
int i = 0;
|
84
|
+
|
85
|
+
while (i < hitColliders.Length)
|
86
|
+
|
87
|
+
{
|
88
|
+
|
89
|
+
Debug.Log(hitColliders[i].gameObject);
|
90
|
+
|
91
|
+
i++;
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
void OnDrawGizmos()
|
100
|
+
|
101
|
+
{
|
102
|
+
|
103
|
+
Gizmos.color = Color.yellow;
|
104
|
+
|
105
|
+
Gizmos.DrawWireSphere(transform.position, sphereRadius); // 常にtransform.positionを見るよう変更
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
```
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
**3点目**
|
118
|
+
|
119
|
+
[OnTriggerEnter](https://docs.unity3d.com/ja/current/ScriptReference/MonoBehaviour.OnTriggerEnter.html)に引数として渡ってくるのは相手の[Collider](https://docs.unity3d.com/ja/current/ScriptReference/Collider.html)だけですが、[OnCollisionEnter](https://docs.unity3d.com/ja/current/ScriptReference/MonoBehaviour.OnCollisionEnter.html)の場合に渡ってくるのは[Collision](https://docs.unity3d.com/ja/current/ScriptReference/Collision.html)です。このオブジェクトには色々な情報が入っていて、相手の[gameObject](https://docs.unity3d.com/ja/current/ScriptReference/Collision-gameObject.html)、[collider](https://docs.unity3d.com/ja/current/ScriptReference/Collision-collider.html)、[rigidbody](https://docs.unity3d.com/ja/current/ScriptReference/Collision-rigidbody.html)、[transform](https://docs.unity3d.com/ja/current/ScriptReference/Collision-transform.html)、他にも...
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
- [contacts](https://docs.unity3d.com/ja/current/ScriptReference/Collision-contacts.html)
|
124
|
+
|
125
|
+
接触した点についての情報が入っている。これら[ContactPoint](https://docs.unity3d.com/ja/current/ScriptReference/ContactPoint.html)から、どの地点でどの向きに接触したか、めり込みの程度はどれだけか...といったことを調べられる。
|
126
|
+
|
127
|
+
- [impulse](https://docs.unity3d.com/ja/current/ScriptReference/Collision-impulse.html)
|
128
|
+
|
129
|
+
めり込みを押し戻す力積を調べられる。特殊な跳ね返りを表現するのに使えるでしょうかね...?
|
130
|
+
|
131
|
+
`Collision`ではなく[Collision2D](https://docs.unity3d.com/ja/current/ScriptReference/Collision2D.html)ですが、[Unity - Unityでbouncinessを変更する方法|teratail](https://teratail.com/questions/112988)では反発係数の変化を表現するのに衝突時の力積を使用しました。
|
132
|
+
|
133
|
+
- [relativeVelocity](https://docs.unity3d.com/ja/current/ScriptReference/Collision-relativeVelocity.html)
|
134
|
+
|
135
|
+
相手と自分の相対速度を調べられる。キャラクター同士がぶつかった速度に応じてダメージ量を変動させる...なんて表現に使えるかもしれません。
|
136
|
+
|
137
|
+
|
138
|
+
|
139
|
+
といったことを調べることができます。難点かも...などと書いたものの、主に物理的応答と関連した情報なので、単に範囲内に入ったかどうかを検知する上では役に立つ場面は少なそうですね。
|
1
DrawWireSphereへのリンクを追加
test
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
まず、球状の領域の可視化には[Gizmos.DrawSphere](https://docs.unity3d.com/ja/current/ScriptReference/Gizmos.DrawSphere.html)を使うのが妥当ではないでしょうか。[Gizmos](https://docs.unity3d.com/ja/2017.4/ScriptReference/Gizmos.html)には他にも色々なシーンビュー上への描画メソッドがあり、デバッグ目的には便利かと思います。
|
1
|
+
まず、球状の領域の可視化には[Gizmos.DrawWireSphere](https://docs.unity3d.com/ja/current/ScriptReference/Gizmos.DrawWireSphere.html)、または[Gizmos.DrawSphere](https://docs.unity3d.com/ja/current/ScriptReference/Gizmos.DrawSphere.html)を使うのが妥当ではないでしょうか。[Gizmos](https://docs.unity3d.com/ja/2017.4/ScriptReference/Gizmos.html)には他にも色々なシーンビュー上への描画メソッドがあり、デバッグ目的には便利かと思います。
|
2
2
|
|
3
3
|
|
4
4
|
|