触れたら落ちるブロックの作成
![825b4d04 0c2d 431e 985b 7ae8c94ffb0f](https://codegenius.org/uploads/slide/image/9064/825b4d04-0c2d-431e-985b-7ae8c94ffb0f.jpeg)
![4c51347e 56ac 4c15 b540 e5b7fe5aec27](https://codegenius.org/uploads/slide/image/9065/4c51347e-56ac-4c15-b540-e5b7fe5aec27.jpeg)
![89da9331 bd2e 4b85 acc4 5c474f2b126d](https://codegenius.org/uploads/slide/image/9066/89da9331-bd2e-4b85-acc4-5c474f2b126d.jpeg)
落ちるブロック
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FallBlock : MonoBehaviour
{
private Rigidbody2D rb2d;
private void Awake()
{
rb2d = GetComponent<Rigidbody2D>();
// ブロックを空中に浮かせる(ポイント)
rb2d.bodyType = RigidbodyType2D.Kinematic;
// 角度を固定する
rb2d.constraints = RigidbodyConstraints2D.FreezeRotation;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
Invoke("Fall", 2);
}
}
void Fall()
{
// 重力が働き出す。
rb2d.bodyType = RigidbodyType2D.Dynamic;
}
}
![39f88f5f bfdb 4e87 8c1b b8a2af9f2b79](https://codegenius.org/uploads/slide/image/9067/39f88f5f-bfdb-4e87-8c1b-b8a2af9f2b79.jpeg)
![C47e8b60 b728 4b7d 88ab 73ee9a4651e4](https://codegenius.org/uploads/slide/image/9068/c47e8b60-b728-4b7d-88ab-73ee9a4651e4.jpeg)
![5290c1ce 57f1 40c1 bf12 61e0f4c40fbe](https://codegenius.org/uploads/slide/image/9069/5290c1ce-57f1-40c1-bf12-61e0f4c40fbe.jpeg)
![0dcd5954 65e2 4e95 abfa 22912ba28434](https://codegenius.org/uploads/slide/image/9070/0dcd5954-65e2-4e95-abfa-22912ba28434.jpeg)
【2022版】ActionGame2D(全33回)
他のコースを見る![825b4d04 0c2d 431e 985b 7ae8c94ffb0f](https://codegenius.org/uploads/slide/image/9064/825b4d04-0c2d-431e-985b-7ae8c94ffb0f.jpeg)
![4c51347e 56ac 4c15 b540 e5b7fe5aec27](https://codegenius.org/uploads/slide/image/9065/4c51347e-56ac-4c15-b540-e5b7fe5aec27.jpeg)
![89da9331 bd2e 4b85 acc4 5c474f2b126d](https://codegenius.org/uploads/slide/image/9066/89da9331-bd2e-4b85-acc4-5c474f2b126d.jpeg)
落ちるブロック
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FallBlock : MonoBehaviour
{
private Rigidbody2D rb2d;
private void Awake()
{
rb2d = GetComponent<Rigidbody2D>();
// ブロックを空中に浮かせる(ポイント)
rb2d.bodyType = RigidbodyType2D.Kinematic;
// 角度を固定する
rb2d.constraints = RigidbodyConstraints2D.FreezeRotation;
}
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Player"))
{
Invoke("Fall", 2);
}
}
void Fall()
{
// 重力が働き出す。
rb2d.bodyType = RigidbodyType2D.Dynamic;
}
}
![39f88f5f bfdb 4e87 8c1b b8a2af9f2b79](https://codegenius.org/uploads/slide/image/9067/39f88f5f-bfdb-4e87-8c1b-b8a2af9f2b79.jpeg)
![C47e8b60 b728 4b7d 88ab 73ee9a4651e4](https://codegenius.org/uploads/slide/image/9068/c47e8b60-b728-4b7d-88ab-73ee9a4651e4.jpeg)
![5290c1ce 57f1 40c1 bf12 61e0f4c40fbe](https://codegenius.org/uploads/slide/image/9069/5290c1ce-57f1-40c1-bf12-61e0f4c40fbe.jpeg)
![0dcd5954 65e2 4e95 abfa 22912ba28434](https://codegenius.org/uploads/slide/image/9070/0dcd5954-65e2-4e95-abfa-22912ba28434.jpeg)
触れたら落ちるブロックの作成