0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Raycastの着地判定で発狂[Unity]

Posted at

記事

ちょっとした手違いを気付かずに一生デバッグしているとき、あると思います。
大体は実装全体像を理解していない故に起きています。
まずは把握を優先しましょう。

ほんへ

着地判定の実装にあたり、今回は自機が回転するので、子オブジェクトに当たり判定を導入するやり方から一新することにしました。
そこでRaycastというのを用いますが、これが大変。
レイヤーの概念など今まで意識したことがないものばかりであったため場当たり的に取り組んでいました。

問題+解決への道

Debug.DrawRayを用いることによって軌跡の視覚化ができていたので、rayが伸びていることは確認できていました。しかし、排他的に地面のレイヤーのみ識別するという処理がまともにできているかの検算ができなかったゆえに、なぜ判定ができないのかを解決できないでいました。

結局、対象Objのレイヤー情報を更新し忘れていただけでした...

rayerではなく段取りを怠った自分を責めました。

以下コードです。

SphereControl.cs
/*
 * Copyright (c) DeepNapEngine
 * https://github.com/TrueRyoB
 */
namespace PoseQ.Core.GamePlay.RhyRhyDaDa
{
    public class SphereControl : MonoBehaviour
    {
        private void FixedUpdate()
        {
            CheckGround();

            if (GameConfig.ShowDebugInfo)
            {
                Debug.DrawRay(
                    transform.position + groundCheckOffsetY * Vector3.up,
                    Vector3.down * _groundCheckDistance,                 
                    _onGround ? Color.green : Color.red
                );
            }
        }
    
        private void CheckGround() => 
            _onGround = Physics.SphereCast(transform.position + groundCheckOffsetY * Vector3.up, groundCheckRadius, Vector3.down, out RaycastHit hit, _groundCheckDistance, groundMask, QueryTriggerInteraction.Ignore);
    }
}

変数の定義や他の関係ない関数などは省略しました。

まとめ

めげずに頑張ってください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?