using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f; // 移動スピード
public float jumpForce = 7f; // ジャンプ力
private Rigidbody2D rb;
private bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
// 左右移動(方向キー ← →)
float move = Input.GetAxis("Horizontal"); // -1 ~ 1
rb.velocity = new Vector2(move * moveSpeed, rb.velocity.y);
// ジャンプ(床にいる時のみ)
if (isGrounded && Input.GetKeyDown(KeyCode.Space))
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
}
}
// 床に触れているかを判定
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = true;
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Ground"))
{
isGrounded = false;
}
}
}