17. 物体検知
17.1 Aのレイキャストによる物体検知
レイキャストは相互作用と異なり、あるゲームオブジェクトから発するレイキャストが相手のゲームオブジェクトに作用させることはありません。物体検知にコライダーは必要はなく、ゲームオブジェクトに対して検知します。
AのレイキャストからBを検知する
AのレイキャストからBを検知させます。シーン上に二つのSphereゲームオブジェクトを置いて、赤いSphereをSphereA、青いSphereをSphereBとします。
SphereAからレイキャストを発射させてBを検知するスクリプトを用意します。 レイキャストを表示するためにDebug.DrawRayメソッドを使って赤色の線で示しています。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RaycastSample : MonoBehaviour
{
public float maxDistance = 10.0f;
Ray ray;
void Start()
{
ray = new Ray( transform.position,
transform.TransformDirection(Vector3.forward));
Debug.DrawRay( gameObject.transform.position,
ray.direction * maxDistance,
Color.red);
if(Physics.Raycast(ray, out RaycastHit hit))
{
//var target = ray.origin + ray.direction * maxDistance;
Debug.Log(hit.collider.gameObject.name);
}
}
}
RaycastSampleコンポーネントをSphereAのゲームオブジェクトに追加します。
SphereAのインスペクタです。
SphereBのインスペクタです。
playを実行して、SphereAから発射されるレイキャストがSphereBに当たりSphereAを検知していることがわかります。
Aのレイキャストで複数のゲームオブジェクトを検知する
Aのレイキャストで複数のゲームオブジェクトを検知させます。シーン上に3つのSphereゲームオブジェクトを置いて、赤いSphereをSphereA、青いSphereをSphereB、ピンク色のSphereをSphereCとします。
SphereAからレイキャストを発射させて複数のゲームオブジェクトを検知するスクリプトを用意します。 RaycastSampleスクリプトと同じくレイキャストを表示するためにDebug.DrawRayメソッドを使って赤色の線で示しています。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class RaycastAll : MonoBehaviour
{
public float maxDistance = 10.0f;
Ray ray;
RaycastHit[] hits;
void Start()
{
ray = new Ray( transform.position,
transform.forward);
hits = Physics.RaycastAll( ray,
maxDistance);
Debug.DrawRay( transform.position,
ray.direction * maxDistance,
Color.red,
1.0f);
if(hits.Length > 0)
{
Array.Sort(hits, (RaycastHit x, RaycastHit y) => x.distance.CompareTo(y.distance));
foreach(var hit in hits)
{
Debug.Log(hit.collider.gameObject.name);
}
}
}
}
RaycastAllコンポーネントをSphereAのゲームオブジェクトに追加します。
SphereAのインスペクタです。
SphereBのインスペクタです。
SphereCのインスペクタです。
playを実行して、SphereAから発射されるレイキャストが複数のゲームオブジェクトを検知していることがわかります。
Aのレイキャストで指定したゲームオブジェクトを検知する
Aのレイキャストで複数のゲームオブジェクトから指定したゲームオブジェクトを検知させます。「Aのレイキャストで複数のゲームオブジェクトを検知する」で使用した3つのSphereゲームオブジェクトを使います。SphereCを指定して検知させます。
Layerに新しく値を追加します。
インスペクタのLayerをクリックしてAdd Layer...をクリックします。
User Layer 7 にSphereが登録されていることを確認します。相互作用の説明で追加していない場合は登録します。
SphereAからレイキャストを発射させて指定したゲームオブジェクトを検知するスクリプトを用意します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class RaycastDetection : MonoBehaviour
{
public float maxDistance = 10.0f;
Ray ray;
RaycastHit[] hits;
void Start()
{
ray = new Ray( transform.position,
transform.forward);
hits = Physics.RaycastAll( ray,
maxDistance,
1 << 7);
Debug.DrawRay( transform.position,
ray.direction * maxDistance,
Color.red,
1.0f);
if(hits.Length > 0)
{
Array.Sort(hits, (RaycastHit x, RaycastHit y) => x.distance.CompareTo(y.distance));
foreach(var hit in hits)
{
Debug.Log(hit.collider.gameObject.name);
}
}
}
}
RaycastDetectionコンポーネントをSphereAのゲームオブジェクトに追加します。
SphereAのインスペクタです。
SphereBのインスペクタです。
SphereCのインスペクタです。LayerをSphereに変更します。
playを実行して、SphereAから発射されるレイキャストが指定したゲームオブジェクトを検知していることがわかります。
レイヤーを複数指定して物体検知することもできます。
SphereBのレイヤーはDefaultにします。
SphereCのレイヤーはSphereにします。
複数のレイヤーを指定して物体検知するために、以下のようにスクリプトを変更します。DefaultレイヤーとSphereレイヤーをもったゲームオブジェクトを検知します。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class RaycastMultiDetection : MonoBehaviour
{
public float maxDistance = 10.0f;
Ray ray;
RaycastHit[] hits;
void Start()
{
ray = new Ray( transform.position,
transform.forward);
hits = Physics.RaycastAll( ray,
maxDistance,
1 << 0 | 1 << 7);
Debug.DrawRay( transform.position,
ray.direction * maxDistance,
Color.red,
1.0f);
if(hits.Length > 0)
{
Array.Sort(hits, (RaycastHit x, RaycastHit y) => x.distance.CompareTo(y.distance));
foreach(var hit in hits)
{
Debug.Log(hit.collider.gameObject.name);
}
}
}
}
playを実行して、SphereAから発射されるレイキャストが指定したゲームオブジェクトを複数検知していることがわかります。
17.2 AのレイキャストがBに当たったときに得られる情報
articulationBody |
The ArticulationBody of the collider that was hit. If the collider is not attached to an articulation body then it is null. |
barycentricCoordinate |
ヒットした三角形の重心座標 |
collider |
ヒットしたコライダー |
colliderInstanceID |
Instance ID of the Collider that was hit. |
distance |
レイの原点から衝突点までの距離 |
lightmapCoord |
衝突点の UV ライトマップ座標 |
normal |
レイがヒットしたサーフェスの法線 |
point |
レイがコライダーにヒットした位置 (ワールド空間)。 |
rigidbody |
ヒットしたコライダーの Rigidbody。コライダーに Rigidbody がアタッチされていない場合は null になります。 |
textureCoord |
The uv texture coordinate at the collision location. |
textureCoord2 |
衝突したセカンダリ UV テクスチャの座標 |
transform |
衝突したコライダーまたは Rigidbody の Transform |
triangleIndex |
衝突したメッシュの三角形におけるインデックス |
🔗 参考リンク
Unity Documentation
RaycastHit