http://codezine.jp/article/detail/8029?p=3
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Openguin : MonoBehaviour {
// ジャンプ力
private float jumpForce = 300f;
private bool grounded = false;
private LayerMask groundLayer = new LayerMask();
private Rigidbody2D rb;
// 初期化時に1回だけ呼ばれる
void Start()
{
rb = GetComponent();
}
// 毎フレーム呼ばれる
void Update () {
print(grounded);
if (Input.GetKeyDown("space") && grounded)
{
rb.AddForce(Vector2.up * jumpForce);
}
}
///
/// ほかのあたり判定と当たったとき
///
///
public void OnCollisionEnter2D(Collision2D col)
{
this.grounded = true;
}
///
/// ほかのあたり判定から離れたとき
///
///
public void OnCollisionExit2D(Collision2D col)
{
this.grounded = false;
}
}