スペースシューター
Space Shooter
【事前学習】前回学んだ機能を再確認しておきましょう。
Unityのチュートリアル「Space Shooter」では、アセットの素材を使ってプロジェクトを作成していく手順を学びます。Unityの多彩な機能を身につけて行ってください。
チュートリアルの進め方
公式ページにはチュートリアルがYouTubeの動画で示されています。本チュートリアルは残念ながら日本語の字幕もありませんが、英単語はUnityで用いられているものや比較的簡単なものですので英語の字幕を参考にして頑張ってください。YouTubeの設定(歯車のアイコン)をクリックして字幕を英語にしてください。また、余力のある方はYouTubeは再生速度を変更できますので、少し速度を落として英語のリスニング能力を養うのも良いでしょう。
Introduction to Space Shooter
最初の動画で本プロジェクトの一連の流れを把握します。
Game Setup, Player and Camera
Setting up the project
(注意事項)
- (テンプレートは3Dで)新しく「Space Shooter」という名前のプロジェクトを生成。シーンは新しく保存せずにSampleSceneを利用します。このビデオまではビデオを閲覧するだけで、以下の操作以外は不要です。
- Asset Storeにつないで、「Space Shooter」で検索して、Space Shooter Tutorialをダウンロード、インポートします。素材
- LayoutはTallがお勧めです。
- Unity の WebPlayerは廃止されており、代替はWebGLが担っていますが、ここではFile/Build Settingsの変更はせずに、通常のPC Standaloneアプリとして開発しましょう。
- Edit/Project Settings/Player/Resolution and PresentationでDefault Is Native Resolutionのチェックを外すと画面解像度を設定できます。幅600pixel、高さ900pixelで生成します。後で Gameタブの直下のDisplay1の右の項目をStandalone(600x900)に変更してください。
Game Setup, Player and Camera
The player GameObject
(注意事項)
- Alt+左ボタンドラッグで視点をピボットポイントを中心に回転します。
- Mesh Collider を追加して Convex,Is Trigger にチェック。
Game Setup, Player and Camera
Camera and lighting
(注意事項)
- Main CameraのPosition(0,10,5)、Rotation(90,0,0)、ProjectionをOrthographic、そのSizeを10に設定します。
- 環境光の設定は Window/Rendering/Lighting Settings で Source の Skybox を Color に変更、Ambient Color を Black(0,0,0) に変更
- Main Light(Directional Light) のRotation(20,-115,0) Intensity0.75 色はWhite(255,255,255)
- Fill Light(Directional Light) のRotation(5,125,0) Intensity0.5 色は水色(128,192,192)
- Rim Light(Directional Light) のRotation(-15,65,0) Intensity0.25 色はWhite(255,255,255)
Game Setup, Player and Camera
Adding a background
(注意事項)
- Hierarcy/Create/Quadで背景(BackGround)を生成、Position(0,-10,0)、Rotation(90,0,0)、Scale(15,30,1)。Mesh Collider を削除。
- 背景のテクスチャをBackGroundオブジェクトにドラッグ&ドロップなどで適用する。
- BackGround の Inspectorにあるテクスチャのコンポーネントの Shader の項目を Standard から Unlit/Texture に変更
Game Setup, Player and Camera
Moving the player
Input Systemを使った PlayerController.cs の簡易版
- Window/PackageManagerで+ボタンの横のプルダウンメニューでUnity Registryに切り替えるとInput Systemが表示されるようになるので、これをInstallする(Installボタンを押す)。BackEndを起動するかと問われるのでyesのボタンを押す。少し時間がかかるがUnityが再起動する。
- Player(vehicle_playerShip) にAddComponentでInput/Player Inputを追加
- (Player Inputコンポーネント内で)Create Actionsボタンでinputactionsファイルを作成し、生成されたアイコンをActionsのフォーム欄にドラッグアンドドロップ
- Playerの子に Assets/Prefabs/VFX/Engines/engines_player.prefab を追加
- PlayerController.csスクリプトを以下のように改変。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerController : MonoBehaviour
{
public float speed = 10.0f;
public float tilt = 4.0f;
private Rigidbody rb;
void Start(){
rb = gameObject.AddComponent<Rigidbody>();
}
private float moveHorizontal = 0f;
private float moveVertical = 0f;
void OnMove(InputValue movementValue){
Vector2 movementVector = movementValue.Get<Vector2>();
moveHorizontal = movementVector.x;
moveVertical = movementVector.y;
}
void FixedUpdate(){
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;
rb.position = new Vector3(
Mathf.Clamp(rb.position.x, -6, 6),
0.0f,
Mathf.Clamp(rb.position.z, -4, 8)
);
rb.rotation = Quaternion.Euler(0.0f, 0.0f, rb.velocity.x * -tilt);
}
}
【事後学習】本日学んだ機能を再確認しておきましょう。