Created
August 26, 2014 23:48
-
-
Save baobao/69e6285dd07274cf5b7a to your computer and use it in GitHub Desktop.
スクリーン座標をワールド座標に変換する
From http://bao-bao.postach.io/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| static public bool TryGetCoordinate (Vector3 mousePosition, Camera cam, out Vector3 worldPosition) | |
| { | |
| // 3D上の座標を取得したい平面を生成 | |
| Plane plane = new Plane (Vector3.up, Vector3.zero); | |
| // インプットスクリーン座標とカメラ位置を通る直線を求める | |
| Ray ray = cam.ScreenPointToRay (mousePosition); | |
| float depth; | |
| // 平面とrayが交差するかチェック | |
| // 第2引数は交差した際に距離(float)が返却される | |
| if (plane.Raycast(ray, out depth)) | |
| { | |
| // ray.origin・・・rayの原点 | |
| // ray.direction・・・rayの方向 | |
| // 単位ベクトルに距離を乗算して原点座標を加算したものが平面と交差した座標となる | |
| worldPosition = ray.origin + ray.direction * depth; | |
| return true; | |
| } | |
| else | |
| { | |
| worldPosition = Vector3.zero; | |
| return false; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment