① プレイヤーの自動右移動スクリプト(2D)
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f; //playerの速度
public float jumpForce = 5f; //playerのジャンプ
private Rigidbody2D rb;
private bool isGrounded = true;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
// 常に右へ移動
rb.velocity = new Vector2(moveSpeed, rb.velocity.y);
// スペースキーでジャンプ
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.velocity = new Vector2(rb.velocity.x, jumpForce);
isGrounded = false;
}
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.collider.CompareTag("Ground"))
{
isGrounded = true;
}
if (collision.collider.CompareTag("Obstacle"))
{
Debug.Log("Game Over");
Time.timeScale = 0; // 一時停止
}
}
}