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?

【Unity】Playerの地面との衝突判定

Posted at

Capsule ColliderをもつPlayerオブジェクトのFixedUpdateで地面との衝突判定をするIsGrounded()を実装した。

private void FixedUpdate()
{
    Move();
    if(IsGrounded())
    {
        jumping = false;
    }
}

private bool IsGrounded(float length = 0.1f) 
{
    Vector3 raycastOrigin = transform.position;
    raycastOrigin.y += .05f;

    if(Physics.Raycast(raycastOrigin, Vector3.down, out RaycastHit hit, length, groundLayer))
    {
        return true;
    }
    return false;
}

はじめは、IsGrounded()の中で、lengthの値と、raycastOrigin.yに加算する値がミスっており上手く衝突判定できなかった。

なので図で描いて整理してみた。

image.png

図の補足

  • 青矢印はray
  • 下の四角がGroundオブジェクト(図のGroudは誤字)
  • Playerオブジェクト自体のtransform.position.yが0になるように、オブジェクトを設定している

NG例とOK例を見比べると、rayの長さは衝突判定対象のColliderの真ん中以上に突っ切らないように調節したほうがよさそうだと思った。

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?