前提・実現したいこと
以下のソースコードにより前面に16本(赤)、内側(黄色)に1本のレイを描画しています。
最終的には足元の黄色いレイで足元のオブジェクトを検出するようにしたいと思っています。
ネット上に画像の様な2Dアクション用のレイ描画のサンプルが見つからなかったので複数のサンプルを切り貼りしており、
レイの描画には成功しましたが、ネット上に紹介されているサンプルの様な衝突判定が使えなくなってしまいました。
該当のソースコード
C#
1 // === レイキャスト用パラメーター ===================== 2 public LayerMask collisionMask; 3 const float skinWidth = .015f; 4 public int horizontalRayCount = 16; 5 public int verticalRayCount = 8; 6 float horizontalRaySpacing; 7 float verticalRaySpacing; 8 new BoxCollider2D collider; 9 RaycastOrigins raycastOrigins; 10 11//レイキャスト関連 12protected virtual void Start() { collider = GetComponent<BoxCollider2D>(); CalculateRaySpacing(); } 13protected virtual void Update() 14{ 15UpdateRaycastOrigins(); 16CalculateRaySpacing(); 17for (int i = 0; i < horizontalRayCount; i++)//前面に出るレイ 18 { 19if (facing == true) {//右を向いたとき 20 //右上からVector2(1,0)*iの間隔でRayを描画。Vector2(0,1)の長さのRAYでカラーは赤を指定 21Debug.DrawRay(raycastOrigins.topRight - Vector2.up * horizontalRaySpacing * i, Vector2.right * 1, Color.red); 22**Debug.DrawRay(raycastOrigins.bottomRight - Vector2.down * horizontalRaySpacing, Vector2.right * -1 / 2, Color.yellow);**//右下内側にレイを描画 23 24RaycastHit hit = new RaycastHit(); 25//レイの衝突判定(エラー部分)-------------------------------------------------------- 26 if (Physics.Raycast(**ray**, out hit))//rayが無い為にエラー 27 { 28 //Rayが当たったオブジェクトのtagがPlayerだったら 29 if (hit.collider.tag == "object") 30 Debug.Log("Rayがobjectに当たった"); 31 } 32//レイの衝突判定(エラー部分)-------------------------------------------------------- 33} 34else if (facing == false){//左を向いたとき 35Debug.DrawRay(raycastOrigins.topLeft - Vector2.up * horizontalRaySpacing * i, Vector2.left * 1, Color.red); 36Debug.DrawRay(raycastOrigins.bottomLeft - Vector2.down * horizontalRaySpacing, Vector2.left * -1 / 2, Color.yellow); 37 } 38} 39for (int i = 0; i < verticalRayCount; i++)//足元に出るレイ 40{ 41Debug.DrawRay(raycastOrigins.bottomLeft + Vector2.right * verticalRaySpacing * i, Vector2.up * -1, Color.red); 42Debug.DrawRay(raycastOrigins.bottomLeft + Vector2.right * verticalRaySpacing * i, Vector2.up * -1, Color.red); 43} 44} 45 46void UpdateRaycastOrigins()//Drawrayで直接角座標(vector2)を定義する事ができない為、ここに定義 47 { 48 Bounds bounds = collider.bounds;//ワールド座標でのコライダーのbounds範囲情報取得 49 bounds.Expand/*設定した amount によって sizeを変更*/(skinWidth/*コライダーの幅*/ * -2); 50 raycastOrigins.bottomLeft = new Vector2(bounds.min.x, bounds.min.y); //左下座標 51 raycastOrigins.bottomRight = new Vector2(bounds.max.x, bounds.min.y); //右下座標 52 raycastOrigins.topLeft = new Vector2(bounds.min.x, bounds.max.y); //左上座標 53 raycastOrigins.topRight = new Vector2(bounds.max.x, bounds.max.y); //左上座標 54 } 55 struct RaycastOrigins 56 { 57 public Vector2 topLeft, topRight, bottomLeft, bottomRight; 58 } 59 void CalculateRaySpacing()//数値に従い幅内のレイ数を振り分ける計算 60 { 61 Bounds bounds = collider.bounds; 62 bounds.Expand(skinWidth * -2); 63 64 horizontalRayCount = Mathf.Clamp(horizontalRayCount, 2, int.MaxValue); 65 verticalRayCount = Mathf.Clamp(verticalRayCount, 2, int.MaxValue); 66 67 horizontalRaySpacing = bounds.size.y / (horizontalRayCount - 1) ; 68 verticalRaySpacing = bounds.size.x / (verticalRayCount - 1); 69 }
発生している問題
変数が足りていないようで、下記ソースの様な衝突判定のソースを使う事が出来ませんでした。
参考)https://qiita.com/nakabonne/items/e37313bfeab77b94210a
C#
1 if (Physics.Raycast(**ray**, out hit))//rayが無い為にエラー 2 { 3 //Rayが当たったオブジェクトのtagがPlayerだったら 4 if (hit.collider.tag == "object") 5 Debug.Log("Rayがobjectに当たった"); 6 }
参考先のこの部分に当たる記述が出来れば解決できると思ったのですが、見当がつきません。
C#
1 Ray ray = new Ray (transform.position, new Vector3 (0, -1, 0));
何か解決策があればご教示お願いします。
補足情報(FW/ツールのバージョンなど)
Unity 2018.1.1f1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/05/28 10:49