0
1

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の作り方 #2

Last updated at Posted at 2020-04-20

解説動画

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

コード

PlayerShip.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


// PlayerShipを方向キーで動かす
// ・方向キーの入力を受け取る
// ・Playerの位置を変更する

// 弾をうつ
// ・弾を作る
// ・弾の動きを作る
// ・発射ポイントを作る
// ・ボタンを押したときにたまを生成する:Instantiate
// -------------//


public class PlayerShip : MonoBehaviour
{
    public Transform firePoint; // 弾を発射する位置
    public GameObject bulletPrefab;

    // 約0.02秒に一回実行される
    void Update()
    {
        Shot();
        Move();
    }

    void Shot()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Instantiate(bulletPrefab, firePoint.position, transform.rotation);
        }
    }
    void Move()
    {
        float x = Input.GetAxisRaw("Horizontal");
        float y = Input.GetAxisRaw("Vertical");
        //課題:GetAxisRawとGetAxisの違いをDebug.Log(x)で調べる
        transform.position += new Vector3(x, y, 0) * Time.deltaTime * 4f;
    }
}

EnemyShip.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

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

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

    void Start()
    {
        
    }

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

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

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

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

EnemyGenerator.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyGenerator : MonoBehaviour
{
    public GameObject enemyPrefab;
    void Start()
    {
        // 繰り返し関数を実行する
        InvokeRepeating("Spawn", 2f, 0.5f);  // Spawn関数を, 2秒後に0.5秒刻みで実行する
    }

    // 生成する
    void Spawn()
    {
        // 生成する位置(x座標)をランダムにしたい
        Vector3 spawnPosition = new Vector3(
            Random.Range(-2.5f, 2.5f),
            transform.position.y,
            transform.position.z
            );

        Instantiate(
            enemyPrefab,        //生成するオブジェクト 
            spawnPosition,      //生成位置
            transform.rotation  //生成時の向き
            );
    }

    void Update()
    {
        
    }
}
Explosion.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Explosion : MonoBehaviour
{
    void Start()
    {
        Destroy(gameObject, 0.5f);
    }
}

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

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

0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?