エッセイマンガ!オタクビアンカップル同棲日記「よめよめ」

同棲中のレズビアンカップルのゆんとあゆむが、日々の生活の様子をマンガや日記にしてお届けするブログです

Unity2Dゲーム制作基本!障害物の設置とゲームオーバー!

 

 

 

    private float SPEED = 0.01f;    // 移動速度
    private float JUMP = 2.0f;    // ジャンプの高さ
    private int ground = 0;    // 接地判定

    [SerializeField] GameObject gameOverText;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        // キャラクター操作
        Vector2 position = transform.position;

        if (Input.GetKey(KeyCode.RightArrow))
        {            
            position.x += SPEED;
        }
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            position.x -= SPEED;
        }
        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            if (ground == 0)
            {
                Debug.Log("Jump");
                position.y += JUMP;
            }
        }

        transform.position = position;
    }

    // トラップ接触判定
    void OnTriggerEnter2D(Collider2D col)
    {
        if (col.tag == "Trap")
        {
            GameOver(); 
        }
    }

    // 接地判定
    void OnTriggerStay2D(Collider2D col)
    {
        if (col.tag == "Ground")
        {
            ground = 0;
        }
    }
    // ジャンプ判定
    void OnTriggerExit2D(Collider2D col)
    {
        if (col.tag == "Ground")
        {
            ground = 1;
        }
    }

    // GameOver処理
    void GameOver()
    {
        gameOverText.SetActive(true);
        this.gameObject.SetActive(false);
        Debug.Log("GameOver");
    }