nomurabbitのブログ

nomurabbitのブログはITを中心にした技術ブログです。

nomurabbitのブログ

【Unity】ゲーム制作の勉強してみた Part1【3DCG】

この記事はUnity 2020.3.26f1を使ったゲーム制作について勉強した内容をまとめたものです。


こんにちは!らびです。今回はUnityでゲーム制作していきます。

頑張って勉強していきましょう!


今回の目標


Part1では世界を作ってプレイヤーを移動させてみます。


f:id:nomurabbit:20220312065331p:plain


各ウインドウを見ていくジョ。

Hierarchyウインドウ


HierarchyウインドウにはFloorとPlayerのCubeオブジェクトを配置します。


f:id:nomurabbit:20220312065813p:plain


いずれもHierarchyウインドウから右クリック、3D Object、Cubeで追加します。


f:id:nomurabbit:20220312065848p:plain

Projectウインドウ


Projectウインドウには必要なAssetを配置します。今回使うのはFloorPlayerMaterial、そしてPlayerの動きを制御するC#スクリプトです。


f:id:nomurabbit:20220312070032p:plain


Material右クリック、Create、Materialで追加します。


f:id:nomurabbit:20220312070212p:plain


C#スクリプト右クリック、Create、C# Scriptで追加します。


f:id:nomurabbit:20220312070420p:plain


C#スクリプトの中身

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

public class PlayerController : MonoBehaviour
{
	public float speed = 2.0f;

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

    // Update is called once per frame
    void Update()
    {
		if (Input.GetKey("up") || Input.GetKey("w"))
		{
			transform.position += transform.forward * speed * Time.deltaTime;
		}
		if (Input.GetKey("down") || Input.GetKey("s"))
		{
			transform.position -= transform.forward * speed * Time.deltaTime;
		}
		if (Input.GetKey("right") || Input.GetKey("d"))
		{
			transform.position += transform.right * speed * Time.deltaTime;
		}
		if (Input.GetKey("left") || Input.GetKey("a"))
		{
			transform.position -= transform.right * speed * Time.deltaTime;
		}
	}
}


キーボードの十字キー もしくはWSADキーで操作できるようになります。

Inspectorウインドウ


Inspectorウインドウでは各オブジェクトAssetを紐づけることができます。PlayerにはMaterialとC#スクリプトを紐づけています。


またRigidbodyというコンポーネントを追加することで、Playerオブジェクトに物理法則を適用します。


f:id:nomurabbit:20220312071050p:plain


ちなみにFloorはこんな感じです。


f:id:nomurabbit:20220312071144p:plain

Sceneウインドウ


次にSceneウインドウです。今回追加したオブジェクトが表示されます。


f:id:nomurabbit:20220312071221p:plain


Y・Z軸Blenderと異なる点に注意だジョ。

Gameウインドウ


最後にGameウインドウです。左上の再生ボタンを押すことで作成したゲームが実行されます。


f:id:nomurabbit:20220312071526p:plain

まとめ


以上、Unityを使ったゲーム制作について勉強した内容のまとめでした。


Unityでできることを増やして、いろんなゲームを作成したいですね。


次回もぜひご覧ください。では!