unityで、簡単なシューティング3Dを作ってみたいと思います。 前回2Dで簡単なシューティングゲームを作りました。 作業の手順はほとんど同じで、Z軸、奥行きの概念を入れると 3Dのシューティングゲームも作れますので 是非、練習してみてください。

▼今回の各オブジェクトの設定

コード

▼PlayerScript

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerScript : MonoBehaviour
{
    //3)弾のプレハブを定義
    public GameObject bulletPrefab;

    // Update is called once per frame
    void Update()
    {
        // 1)入力を取得(←→キー、↑↓キー)
        float moveX = Input.GetAxis("Horizontal");// 左右
        float moveY = Input.GetAxis("Vertical");// 上下

        //2)移動実行
        //transform.Translate(Vector3 移動量)  ⇒自分の視点からの相対移動
        //transform.position += new Vector3(x,y,z)  ⇒座標を指定する絶対移動
        transform.position += new Vector3(moveX, moveY, 0f) *10f * Time.deltaTime;

        //4)スペースキーで弾生成
        if (Input.GetKeyDown(KeyCode.Space))
        {
            //Instantiate(何を, 位置, 回転);
            Instantiate(bulletPrefab, transform.position, transform.rotation);
        }

    }

    //5)敵と当たったら
    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("EnemyTag"))
        {
            GetComponent<Renderer>().material.color = Color.red;
            other.GetComponent<Renderer>().material.color = Color.red;
            Time.timeScale = 0f;//ゲームを止める
        }

    }
}

▼BulletScript

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BulletScript : MonoBehaviour
{
   
    // Update is called once per frame
    void Update()
    {
        //1)前方に発射
        transform.position += Vector3.forward *20f *Time.deltaTime;

        // 2)画面外で削除
        if(transform.position.z >= 15f)
        {
            Destroy(gameObject);
        }
    }

    //3)当たったら
    private void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("EnemyTag"))
        {
            Destroy(other.gameObject); // 敵を消す
            Destroy(gameObject); // 自身を消す
        }
        
    }
}

▼EnemyScript

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyScript : MonoBehaviour
{
 
    // Update is called once per frame
    void Update()
    {
        //1)手前方向へ移動
        transform.position += Vector3.back * 5f * Time.deltaTime;

        //2)枠から出たら削除
        if (transform.position.z <= -15f)
        {
            Destroy(gameObject);
        }

    }
}

▼GameManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    //1)敵のプレハブを定義
    public GameObject enemyPrefab;

    // Start is called before the first frame update
    void Start()
    {
        //3)InvokeRepeating("実行する関数名", 開始時間, 間隔);
        InvokeRepeating("CreateEnemy", 1f, 3f);

    }

    //2)敵出現の関数
    void CreateEnemy()
    {
        float enemyX = Random.Range(-10f, 10f);
        float enemyY = Random.Range(3f, 6f);
        //敵を生成する座標
        Vector3 enemyPos = new Vector3(enemyX, enemyY, 15f);

        //Instantiate(何を, 位置, 回転);
        Instantiate(enemyPrefab, enemyPos, transform.rotation);
    }
}

コメントを残す