teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

コメントに対して追記

2018/11/24 23:00

投稿

Bongo
Bongo

スコア10816

answer CHANGED
@@ -1,4 +1,70 @@
1
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
  衝突相手の情報を得るのでしたら、[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)を使ってみてはいかがでしょう。
4
- ただ、球領域と接触するコライダーは得られるものの、[OnCollisionEnter](https://docs.unity3d.com/ja/current/ScriptReference/MonoBehaviour.OnCollisionEnter.html)のような衝突に関する詳しい情報は得られないのは難点かもしれませんね。どちらかというと[OnTriggerEnter](https://docs.unity3d.com/ja/current/ScriptReference/MonoBehaviour.OnTriggerEnter.html)に近い動作になるかと思います。
4
+ ただ、球領域と接触するコライダーは得られるものの、[OnCollisionEnter](https://docs.unity3d.com/ja/current/ScriptReference/MonoBehaviour.OnCollisionEnter.html)のような衝突に関する詳しい情報は得られないのは難点かもしれませんね。どちらかというと[OnTriggerEnter](https://docs.unity3d.com/ja/current/ScriptReference/MonoBehaviour.OnTriggerEnter.html)に近い動作になるかと思います。
5
+
6
+ #コメントについて追記
7
+ **1点目**
8
+ 空のオブジェクトでも描画されるはずですが、なぜでしょうかね...?
9
+ 可能性としては、この`Sample`スクリプトでは、ギズモ描画位置の決定にフィールド`center`を使っています。この`center`にオブジェクトの座標がセットされるのは`Start`メソッドが実行された後ですので、シーン編集中の段階では`center`に座標がセットされず`(0, 0, 0)`のままとなり、ワールド原点に球が描画されてしまっている...ということがありそうな気がします。ワールド原点がシーンビューの画面外に出てしまっている場合、一見何も描画されなかったように見えてしまうかもしれません。
10
+ ワールド原点に球が描画されているか確認してみたり、あるいはプレイボタンを押して実行してみてください。プレイモードにすると`center`にオブジェクトの座標がセットされ、オブジェクトの位置に球が現れるかと思います。
11
+ もし、プレイモードにしてもやっぱりオブジェクトの位置に球が現れないようなら、なにか他の原因でしょうね...その場合はコメントいただければ、さらに調査してみようと思います。
12
+
13
+
14
+ **2点目**
15
+ 1点目とも関連している話ですが、`center`がセットされるのは`Start`実行時の一回きりです。ですので実行中にオブジェクトを動かそうが、球の位置は`Start`時の位置から変わらないはずです。
16
+ もし常にオブジェクトの座標に追従させたいのでしたら、そもそも`center`を覚えておくのはやめて、常に最新の位置を球の中心にしてしまってはいかがでしょうか?
17
+ ```C#
18
+ using System.Collections;
19
+ using System.Collections.Generic;
20
+ using UnityEngine;
21
+
22
+ public class Sample : MonoBehaviour
23
+ {
24
+
25
+ [SerializeField]
26
+ float sphereRadius = 3.0f;
27
+ // Vector3 center; // centerは廃止
28
+
29
+ // Startも不要なので削除
30
+ /*
31
+ // Use this for initialization
32
+ void Start()
33
+ {
34
+ center = this.transform.position;
35
+ }
36
+ */
37
+
38
+ // Update is called once per frame
39
+ void Update()
40
+ {
41
+ Collider[] hitColliders = Physics.OverlapSphere(transform.position, sphereRadius); // 常にtransform.positionを見るよう変更
42
+ int i = 0;
43
+ while (i < hitColliders.Length)
44
+ {
45
+ Debug.Log(hitColliders[i].gameObject);
46
+ i++;
47
+ }
48
+ }
49
+
50
+ void OnDrawGizmos()
51
+ {
52
+ Gizmos.color = Color.yellow;
53
+ Gizmos.DrawWireSphere(transform.position, sphereRadius); // 常にtransform.positionを見るよう変更
54
+ }
55
+ }
56
+ ```
57
+
58
+
59
+ **3点目**
60
+ [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)、他にも...
61
+
62
+ - [contacts](https://docs.unity3d.com/ja/current/ScriptReference/Collision-contacts.html)
63
+ 接触した点についての情報が入っている。これら[ContactPoint](https://docs.unity3d.com/ja/current/ScriptReference/ContactPoint.html)から、どの地点でどの向きに接触したか、めり込みの程度はどれだけか...といったことを調べられる。
64
+ - [impulse](https://docs.unity3d.com/ja/current/ScriptReference/Collision-impulse.html)
65
+ めり込みを押し戻す力積を調べられる。特殊な跳ね返りを表現するのに使えるでしょうかね...?
66
+ `Collision`ではなく[Collision2D](https://docs.unity3d.com/ja/current/ScriptReference/Collision2D.html)ですが、[Unity - Unityでbouncinessを変更する方法|teratail](https://teratail.com/questions/112988)では反発係数の変化を表現するのに衝突時の力積を使用しました。
67
+ - [relativeVelocity](https://docs.unity3d.com/ja/current/ScriptReference/Collision-relativeVelocity.html)
68
+ 相手と自分の相対速度を調べられる。キャラクター同士がぶつかった速度に応じてダメージ量を変動させる...なんて表現に使えるかもしれません。
69
+
70
+ といったことを調べることができます。難点かも...などと書いたものの、主に物理的応答と関連した情報なので、単に範囲内に入ったかどうかを検知する上では役に立つ場面は少なそうですね。

1

DrawWireSphereへのリンクを追加

2018/11/24 23:00

投稿

Bongo
Bongo

スコア10816

answer 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
  衝突相手の情報を得るのでしたら、[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)を使ってみてはいかがでしょう。
4
4
  ただ、球領域と接触するコライダーは得られるものの、[OnCollisionEnter](https://docs.unity3d.com/ja/current/ScriptReference/MonoBehaviour.OnCollisionEnter.html)のような衝突に関する詳しい情報は得られないのは難点かもしれませんね。どちらかというと[OnTriggerEnter](https://docs.unity3d.com/ja/current/ScriptReference/MonoBehaviour.OnTriggerEnter.html)に近い動作になるかと思います。