Skip to content

Instantly share code, notes, and snippets.

@ys4kw
Last active December 20, 2015 02:59
Show Gist options
  • Save ys4kw/6060422 to your computer and use it in GitHub Desktop.
Save ys4kw/6060422 to your computer and use it in GitHub Desktop.
マウスカーソル下の3次元上のオブジェクトを取得するサンプル
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
float distance = 100;
if (collider.Raycast (ray, out hit, distance)) {
	Debug.Log ("Hit! Object name is " + hit.transform.name);
}

Rayの作成

Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

この場合、メインカメラからRayを作成する

衝突の検出

collider.Raycast (ray, out hit, distance)

Trueが返ってくれば検出成功 その場合、hitに衝突したColliderの情報が入る

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment