0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Unity:2DShootingの作り方 #3

Posted at

解説動画

・前回:https://qiita.com/simanezumi1989/items/d6c6d9358dd5d096f03f
・コードの差分可視化サイト:https://www.diffchecker.com

コード

GameController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

// スコアの実装
// ・UIの作成
// ・UIの更新
// ・敵と弾がぶつかったときにスコア加算+更新
// ・Playerと弾の差別化:Tag

// ゲームオーバーの実装
// ・UIを作成
// ・敵とPlayerがぶつかったときにUIを表示
// ・リトライの実装
//  ・Spaceを押したらシーンを再読み込み

public class GameController : MonoBehaviour
{
    public GameObject gameOverText;

    public Text scoreText;
    int score = 0;
    void Start()
    {
        gameOverText.SetActive(false);
        scoreText.text = "SCORE:" + score;
    }

    private void Update()
    {
        if (gameOverText.activeSelf == true)
        {
            if (Input.GetKeyDown(KeyCode.Space))
            {
                SceneManager.LoadScene("Main");
            }
        }
    }

    // スコア加算
    public void AddScore()
    {
        score += 100;
        scoreText.text = "SCORE:" + score;
    }

    public void GameOver()
    {
        gameOverText.SetActive(true);
    }
}
EnemyShip.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

// 敵の移動:ましたに移動する
// 敵を生成:生成工場を作ってやる
// 敵に弾が当たったら爆発する
// 敵とPlayerがぶつかったら爆発する
// ------
// 敵を左右に揺らせる
// スコアの表示
// 敵を倒したときにスコアを上昇させる
// リスタートの実装

public class EnemyShip : MonoBehaviour
{
    public GameObject explosion; // 破壊のプレファブ

    // GameControllerの入れ物を作る:AddScoreを使いたいから
    GameController gameController; 
    void Start()
    {
        // GameObject.Find("GameController")
        //・ヒエラルキー上のGameControllerという名前のオブジェクトを取得
        gameController = GameObject.Find("GameController").GetComponent<GameController>();
    }

    void Update()
    {
        // 敵の移動:ましたに移動する
        transform.position -= new Vector3(0, Time.deltaTime, 0);
    }

    // 敵に弾が当たったら爆発する
    // 当たり判定の基礎知識:
    // 当たり判定を行うには、
    // ・両者にColliderがついている
    // ・少なくともどちらかにRigidbodyがついている

    // isTriggerにチェックをつけた場合はこちらが実行される
    private void OnTriggerEnter2D(Collider2D collision)
    {
        // collisionにぶつかった相手の情報が入っている:bullet, Player
        if (collision.CompareTag("Player") == true)
        {
            Instantiate(explosion, collision.transform.position, transform.rotation);
            gameController.GameOver();
        }
        else if (collision.CompareTag("Bullet") == true)
        {
            gameController.AddScore();
        }
        Instantiate(explosion, transform.position, transform.rotation);
        Destroy(gameObject);
        Destroy(collision.gameObject);
    }

    /*
    private void OnCollisionEnter2D(Collision2D collision)
    {  
    }
    */
}

スタジオしまづからのお知らせ

スタジオしまづ は「プログラミング未経験でもゲームをリリース」をコンセプトに集まるオンラインサロンを運営しています。
https://camp-fire.jp/projects/view/149191
対象は以下の方としています。興味のある方はぜひ!
・独学でゲームを作れるか心配な方
・コミケで出展側として参加したい方
・一度でいいから自分のゲームを世に出したい方
・ゲームを作って憧れのゲーム会社に就職したい方

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?