導入
入力バッファは着地前に押した入力を0.12秒だけ保持します。コヨーテタイムは足場を離れた後0.1秒だけジャンプを許可します。入力と接地の時刻を別々に覚え、ジャンプ成功で両方の猶予を消費します。
準備と配置
PlayerにRigidbodyとColliderを付け、足元の子TransformをgroundCheckへ登録します。床のLayerをGroundにし、groundMaskへGroundだけを入れます。Player自身はGroundから除外します。RigidbodyはIs Kinematicをオフ、必要に応じて回転を固定します。
Unity 6向けの例です。[SerializeField]を付けた参照や設定値をInspectorで登録します。
旧Input APIを使う例です。Project Settings > Player > Active Input HandlingでInput Manager (Old)またはBothを選びます。Input Systemのみにしたプロジェクトではそのまま使えません。
処理の流れ
- 着地前の入力: 0.12秒保持
- 離地後の猶予: 0.1秒
- 両条件が成立: ジャンプ
完成版
以下は記事内の最小例です。機能の範囲は後述の「この例の範囲」で確認してください。
JumpAssist.cs
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class JumpAssist : MonoBehaviour
{
[SerializeField]
Transform groundCheck;
[SerializeField]
LayerMask groundMask;
[SerializeField, Min(.01f)]
float groundRadius = .15f;
[SerializeField]
float jumpPower = 6f, bufferSeconds = .12f, coyoteSeconds = .1f;
Rigidbody body;
float lastPressed = float.NegativeInfinity, lastGrounded = float.NegativeInfinity;
bool jumpConsumed;
void Awake()
{
body = GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetButtonDown("Jump"))
lastPressed = Time.time;
}
void FixedUpdate()
{
bool grounded = body.linearVelocity.y <= .1f && Physics.CheckSphere(groundCheck.position, groundRadius, groundMask, QueryTriggerInteraction.Ignore);
if (grounded)
{
lastGrounded = Time.time;
jumpConsumed = false;
}
bool buffered = Time.time - lastPressed <= bufferSeconds;
bool canCoyote = Time.time - lastGrounded <= coyoteSeconds;
if (buffered && canCoyote && !jumpConsumed)
{
body.linearVelocity = new Vector3(body.linearVelocity.x, jumpPower, body.linearVelocity.z);
lastPressed = lastGrounded = float.NegativeInfinity;
jumpConsumed = true;
}
}
}
Inspectorと呼び出しの接続
登録する参照・設定値: groundCheck、groundMask、groundRadius、jumpPower、bufferSeconds、coyoteSeconds。
動作確認
着地の少し前のSpaceと、足場から出た直後のSpaceを別々に試します。猶予を0にして違いを比較し、空中の連打で追加ジャンプしないことを確認します。
この例の範囲
Unity 6の3D物理と旧Input ManagerのJumpを使います。groundCheckの球が足元より上に大きく入り込むと誤接地するため、Gizmosで位置と半径を調整します。
まとめ
入力バッファは着地前に押した入力を0.12秒だけ保持します。コヨーテタイムは足場を離れた後0.1秒だけジャンプを許可します。入力と接地の時刻を別々に覚え、ジャンプ成功で両方の猶予を消費します。
この記事のまとめ
押したはずなのに反応しない感覚を減らすために、先行入力、猶予時間、成功時の返し方を初心者向けに整理します。
